A bit of description: The first time when you are accessing the page (index.php) the data is loaded into the combobox. Each option tag receives the client number as value (C_no) and other details, as text.
On the other hand, when you are selecting a value in the combobox, you need to fetch additional client data from the database. For this you need jquery and ajax. With it, when the value of the combobox changes, you must make an ajax request to the server (getClient.php). As response it sends you the corresponding data from the database table. You then take the data and put it wherever you like - in this case in the inputs.
A bit of suggestion: I would recommend you to start using prepared statements - in order to avoid sql injection - and to not mix php code for fetching data from db with html code. Just separate them. First, fetch data on top of the page and save it into an array. Then, inside the html code, use only the array to iterate through fetched data.
index.php
<?php
require 'connection.php';
$sql = 'SELECT C_no, Cname, Caddress FROM Client_table ORDER BY Cname ASC';
// Prepare the statement.
$statement = mysqli_prepare($connection, $sql);
// Execute the statement.
mysqli_stmt_execute($statement);
// Get the result set from the prepared statement.
$result = mysqli_stmt_get_result($statement);
// Fetch the data and save it into an array for later use.
$clients = mysqli_fetch_all($result, MYSQLI_ASSOC);
// Free the memory associated with the result.
mysqli_free_result($result);
// Close the prepared statement. It also deallocates the statement handle.
mysqli_stmt_close($statement);
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" />
<meta charset="UTF-8" />
<!-- The above 3 meta tags must come first in the head -->
<title>Demo</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#name').change(function (event) {
var cNo = $(this).val();
if (cNo === 'Select') {
$('#address').val('');
$('#contact').val('');
} else {
$.ajax({
method: 'post',
dataType: 'json',
url: 'getClient.php',
data: {
'cNo': cNo
},
success: function (response, textStatus, jqXHR) {
if (!response) {
alert('No client data found.');
} else {
$('#address').val(response.address);
$('#contact').val(response.contact);
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert('An error occurred. Please try again.');
},
cmplete: function (jqXHR, textStatus) {
//...
}
});
}
});
});
</script>
<style type="text/css">
body {
padding: 30px;
}
input, select {
display: block;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="container">
<select name="name" id="name" class="form-control">
<option value="Select">- Select -</option>
<?php
foreach ($clients as $client) {
?>
<option value="<?php echo $client['C_no']; ?>">
<?php echo $client['Cname'] . ' (' . $client['Caddress'] . ')'; ?>
</option>
<?php
}
?>
</select>
<label>Address</label>
<input type="text" id="address" name="address"/>
<label>Contact</label>
<input type="text" id="contact" name="contact"/>
</div>
</body>
</html>
getClient.php
<?php
require 'connection.php';
// Validate posted value.
if (!isset($_POST['cNo']) || empty($_POST['cNo'])) {
echo json_encode(FALSE);
exit();
}
$cNo = $_POST['cNo'];
$sql = 'SELECT
Caddress AS address,
Ccontact AS contact
FROM Client_table
WHERE C_no = ?
LIMIT 1';
// Prepare the statement.
$statement = mysqli_prepare($connection, $sql);
/*
* Bind variables for the parameter markers (?) in the
* SQL statement that was passed to prepare(). The first
* argument of bind_param() is a string that contains one
* or more characters which specify the types for the
* corresponding bind variables.
*
* @link http://php.net/manual/en/mysqli-stmt.bind-param.php
*/
mysqli_stmt_bind_param($statement, 'i', $cNo);
// Execute the statement.
mysqli_stmt_execute($statement);
// Get the result set from the prepared statement.
$result = mysqli_stmt_get_result($statement);
// Fetch the data and save it into an array for later use.
$clientDetails = mysqli_fetch_array($result, MYSQLI_ASSOC);
// Free the memory associated with the result.
mysqli_free_result($result);
// Close the prepared statement. It also deallocates the statement handle.
mysqli_stmt_close($statement);
if (!isset($clientDetails) || !$clientDetails) {
echo json_encode(FALSE);
} else {
echo json_encode($clientDetails);
}
exit();
connection.php
<?php
// Db configs.
define('HOST', 'localhost');
define('PORT', 3306);
define('DATABASE', 'your-db');
define('USERNAME', 'your-user');
define('PASSWORD', 'your-pass');
// Display eventual errors.
error_reporting(E_ALL);
ini_set('display_errors', 1); /* SET IT TO 0 ON A LIVE SERVER! */
// Enable internal report functions.
$mysqliDriver = new mysqli_driver();
$mysqliDriver->report_mode = (MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
// Create db connection.
$connection = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE, PORT);
Create table syntax
DROP TABLE IF EXISTS `Client_table`;
CREATE TABLE `Client_table` (
`C_no` int(11) unsigned NOT NULL AUTO_INCREMENT,
`Cname` varchar(100) DEFAULT NULL,
`Caddress` varchar(100) DEFAULT NULL,
`Ccontact` varchar(100) DEFAULT NULL,
PRIMARY KEY (`C_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `Client_table` (`C_no`, `Cname`, `Caddress`, `Ccontact`)
VALUES
(1,'Mohit','xyz','0123645789'),
(2,'Ramesh','abc','7485962110'),
(5,'Tanja','def','1232347589'),
(6,'Paul','pqr','0797565454'),
(7,'Mohit','klm','0123645789');
Mohithas two records in the table, then he has two different addresses, or two different contact numbers. One in each record. Then it would be wrong to make aDISTINCT, because then you would fetch only one address (or contact phone) into your combobox...