I wrote a script to filter though a database and select all the records that contain certain value. I have an AJAX dropdown to select the value. However when I make a selection in the dropdown it does not return any results. Any help would be appreciated. There are two code snippets the index.php is where the dropdown and table output are. Type.php does the database query and sends the results back to the index.php
Index.php
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<form>
<?php
include ("includes/dbh.inc.php");
$type = "SELECT typeName FROM types";
$stmt = $mysqli->prepare($type);
$stmt ->execute();
$stmt ->bind_result($typeName);
$stmt ->store_result();
echo '<select onchange="showType(this.value)" name"showType">';
echo '<option>Select Car Type</option>';
while($stmt->fetch()){
echo '<option value="'.$typeName.'"">'.$typeName.'</option>';
}
echo '</select>';
$stmt ->close();
<a href="index.php">Show All</a>
</form>
<table id="carList" border="1">
<thead>
<tr>
<th>picture</th>
<th width="500">Details</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$("#typeName").on("change", function() {
$.ajax({
type: "POST",
data: {
"typeName": $("#typeName").val()
},
url:"type.php",
dataType: "json",
success: function(JSONObject){
var carHTML = "";
for (var key in JSONObject){
if(JSONObject.hasOwnProperty(key)){
carHTML += "<tr>";
carHTML +="<td>" + JSONObject[key]["stockName"] + "</br>"
carHTML += JSONObject[key]["stockPack"] + "</td>";
carHTML += "</tr>";
}
}
$("#carList tbody").html(carHTML);
}
});
});
</script>
Type.php
<?php
include_once 'includes/dbh.inc.php';
$sql = "SELECT * FROM rollingstock WHERE stockType=?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)){
echo "SQL statment failed";
} else {
mysqli_stmt_bind_param($stmt, "s", $_POST["typeName"]);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$carArray = [];
while ($row = mysqli_fetch_assoc($result)){
array_push($carArray, [
'stockName' => $row['stockName'],
'stockPack' => $row['stockPack']
]);
}
}
$JSONoutput = json_encode($carArray);
echo $JSONoutput;