In my little application I have owner table in my database. Every owner has his ID, name, address and description (those are columns of this table).
On my website I have select element where you can choose owners.
Under this select I have 3 inputs (one for name, one for address and one for description).
When I change the option in my select element I would like to fill all inputs with the data from my database using AJAX.
My PHP:
<?php
$sql_select=$mysqli->prepare("SELECT id, name FROM owner ORDER BY id DESC");
$sql_select->execute();
$sql_select->store_result();
$sql_select->bind_result($id_owner, $name_owner);
$select = $_POST['owner'];
echo "<option value=0 ".selected.">Select...</option>";
while($sql_select->fetch()) {
if($select == $id_owner){
$selected="selected";
}
else {
$selected="";
}
echo "<option value=".$id_owner." ".$selected." data-id=".$id_owner.">".$id_owner." ".$name_owner."</option>";
}
?>
<label for="owner_name">Owner name</label>
<input id="owner_name" name="owner_name" type="text">
<label for="owner_address">Owner address</label>
<input id="owner_address" name="owner_address" type="text">
<label for="owner_description">Owner description</label>
<input id="owner_description" name="owner_description" type="text">
Rendered HTML:
<select id="owner" name="owner">
<option value="0" selected>Select...</option>
<option value="1" data-id="1">1 ABC</option>
<option value="2" data-id="2">2 DEF</option>
<option value="3" data-id="3">3 GHI</option>
</select>
<label for="owner_name">Owner name</label>
<input id="owner_name" name="owner_name" type="text">
<label for="owner_address">Owner address</label>
<input id="owner_address" name="owner_address" type="text">
<label for="owner_description">Owner description</label>
<input id="owner_description" name="owner_description" type="text">
This above code reads data from MySQL and creates options for select.
So far so good.
I was able to achieve my goal by setting data-name, data-address and data-description attributes in my HTML and assign to this 3 attributes proper data from the database to every option in my select and then use jQuery to fill the inputs. It looks like this after rendering PHP:
HTML:
<option value="0" selected>Select...</option>
<option value="1" data-id="1" data-name="ABC" data-address="London" data-description="Description of ABC owner">1 ABC</option>
<option value="2" data-id="2" data-name="DEF" data-address="Birmingham" data-description="Description of DEF owner">2 DEF</option>
<option value="3" data-id="3" data-name="GHI" data-address="Manchester" data-description="Description of GH~I owner">3 GHI</option>
JS:
$('#owner').change(function () {
var changedOption = $(this).find('option:selected');
var ownerName = changedOption.attr('data-name');
var ownerAddress = changedOption.attr('data-address');
var ownerDescription = changedOption.attr('data-description');
$('#owner_name').val(ownerName);
$('#owner_address').val(ownerAddress);
$('#owner_description').val(ownerDescription);
});
But there is a problem, imagine I have 1000 owners and think how much data would have to be assign into HTML. I would like to read data from my database on every change of my select using AJAX to avoid using HTML data-attributes.
Little example on CODEPEN to see what I mean exactly.
Can someone give me a clue where to start?