我尝试使用下拉列表在WordPress页面中显示我的数据库中的一些信息(Adresse),但是当我从下拉列表中选择一个项目(Nom)时,它什么也没显示。 我确实在这里按照了正确的答案:ajax call to populate form fields from database query when select value changes 所以我不知道我在这里犯的错误:
下拉列表代码:
<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>
onchange函数我将它插入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>
formdata.php代码:
<?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 ;
?>