William Goatley William Goatley - 2 months ago 7
Ajax Question

User populated box will not change if input changes

The drop down is populated from a database "meal_planner I am trying to get the drop down to populate the rest of the row based on the users selection. Currently, only the first selection works; if anything else is selected, it still uses the first choice. I just kind of pieced this together.

<?php
$link=mysqli_connect("localhost", "root", "");
mysqli_select_db($link,"meal_planner");
?>
<!DOCTYPE html>
<html>
<head>
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
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 (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>

<form name="form1" action="" method="post">
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<?php
$res=mysqli_query($link,"select name from protein");
while($row=mysqli_fetch_array($res))
{
?>
<option><?php echo $row["name"]; ?></option>

<?php
}

?>

</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>

</body>
</html>


getuser.php

<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}

table, td, th {
border: 1px solid black;
padding: 5px;
}

th {text-align: left;}
</style>
</head>
<body>

<?php
$q = intval($_GET['q']);

$con = mysqli_connect('localhost','root','','meal_planner');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM protein WHERE protein_id = '".$q."'";
$result = mysqli_query($con,$sql);

echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['calories'] . "</td>";
echo "<td>" . $row['protein'] . "</td>";
echo "<td>" . $row['carbs'] . "</td>";
echo "<td>" . $row['fats'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>

Answer

You didn't put the protein_id into the <option> elements.

$res=mysqli_query($link,"select name, protein_id from protein");
while($row=mysqli_fetch_array($res))
{
?>
<option value="<?php echo $row["protein_id"]; ?>"><?php echo $row["name"]; ?></option>
<?php
}

When there's no value attribute, it uses the text of the element, so you're ending the protein name as the select value. Then when you use intval($q), it returns 0 because the protein name is not a valid integer, and the query in the AJAX call always gets the protein with protein_id = 0.