Hello Help Much Appreciated. I am having trouble figuring this out. I cannot find tutorials anywhere with this specific task. I have one table with ID, Manufacturer, ModelNo, Manupartcode and more fields.
I want first drop down list to be populated with Manufacturer. SELECT Manufacturer FROM Table_Name; This is fine
The second list need to be ModelNo. SELECT ModelNo FROM Table_Name WHERE Manufacturer = 'What We picked from first list';
INDEX.PHP
<!DOCTYPE html>
<html>
<head>
<script>
function showData(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 (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET","showData.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<form>
<?php
$conn = new mysqli('Server_Name', 'Username', 'Password', 'Database_Name')
or die ('Cannot connect to db');
$result = $conn->query("select DISTINCT Manufacturer from Table_Name");
echo "<select name = 'Manufacturer' onchange = 'showData(this.value)'>";
while ($row = $result->fetch_assoc()) {
unset($Manufacturer);
$Manufacturer = $row['Manufacturer'];
echo '<option value="'.$Manufacturer.'">'.$Manufacturer.'</option>';
}
echo "</select>";
?>
</form>
<br>
<div id="txtHint">
Text
</div>
</body>
</html>
SHOWDATA.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('Server_Name', 'Username', 'Password', 'Database_Name');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"Database_Name");
$sql="SELECT ModelNo FROM Table_Name WHERE Manufacturer = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>ModelNo</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['ModelNo'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>