I have a Dropdown list that is populated by MySQL database using the following code
<?php
$conn = new mysqli('localhost', 'root', '@', '')
or die ('Cannot connect to db');
$result = $conn->query("select name, surname FROM clients");
echo "<html>";
echo "<body>";
echo "<select name='client_name' id='client'>";
while ($row = $result->fetch_assoc()) {
unset($msisdn, $contract_type, $provider);
$name = $row['name'];
$surname = $row['surname'];
echo '<option value="'.$name.'_'.$surname.'">'.$name.' '.$surname.'</option>';
}
echo "</select>";
echo "</body>";
echo "</html>";
?>
I want to populate different fields with information related to the result in the drop down list.
<input type="text" name="address">
<input type="text" name="tel">
<input type="text" name="id_nr">
$('#client').change(function() {
selectedOption = $('option:selected', this);
$('input[name=address]').val( selectedOption.data('address') );
$('input[name=tel]').val( selectedOption.data('tel') );
$('input[name=id_nr]').val( selectedOption.data('id_nr') );
});
You should set data to option as
echo '<option data-address-"'.$address.'" value="'.$name.'_'.$surname.'">'.$name.' '.$surname.'</option>';
Also, update your query to get address along with name & surname.