I try to display some information (Adresse) from My database in a WordPress page using a drop-down list, but when I select an item (Nom) from the drop-down list it displays nothing. I did follow the right answer here: ajax call to populate form fields from database query when select value changes So i don't know wht error i made here :
The dropdown list code:
<select class='Nom' onchange="showUser(this.value)" name='Nom' id='Nom'>
<option value="">--- Select ---</option>
[insert_php]
$servername = "localhost";
$username = "root";
$password = "";
$conn = new mysqli($servername, $username, $password, "somapam_bd");
$sql = mysqli_query($conn, "SELECT Nom FROM herboristes");
while($ligne_liste=mysqli_fetch_array($sql)) {
echo '<option value="'.$ligne_liste['Nom'].'">'.$ligne_liste['Nom']."</option>\n";
}
echo '</select>';
[/insert_php]
<div id="txtHint" type="text" name="txtHint"><b>Person info will be listed here...</b></div>
the onchange function i insert it in the index.php :
<script>
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").value="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").value = this.responseText;
}
}
xmlhttp.open("GET","formdata.php?q="+str,true);
xmlhttp.send();
}
</script>
The formdata.php code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$conn = new mysqli($servername, $username, $password, "somapam_bd");
$q = intval($_GET['q']);
$result = mysqli_query($conn, "SELECT * FROM herboristes where Nom = '".$q."'");
while($row=mysqli_fetch_array($result))
{
$cID = $row['Adresse'];
}
echo $cID ;
?>