当工厂选择下拉列表更改为1或2时,我尝试运行此AJAX脚本,发送值get_option为1或2.它将运行查询以获取与之一起使用的工具列表那个工厂号码。工具选择下拉列表未填充。
$(document).ready(function () {
function fetch_select(val) {
$.ajax({
type: "POST",
url: "fetch_data.php",
data: {
get_option:val
},
success: function (response) {
$("toolselect").append(response);
}
});
}
$("#plantselect").on("change", this, function(e) {
fetch_select($(this).val())
});
}
所有内容似乎都发送正常,并从fetch_data.php页面接收200响应。
<?php
if(isset($_POST['get_option']))
{
$conn = mysqli_connect('localhost', 'x', 'x', 'x');
if (!$conn) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($conn) . PHP_EOL;
$plant = $_POST['get_option'];
$sql="SELECT toolnum FROM tools WHERE plant='$plant'";
$stmt = mysqli_query($conn, $sql);
while ($d = mysqli_fetch_assoc($stmt)) {
echo "<option value='".$d['toolnum']."'>".$d['toolnum']."</option>";
}
/* Free statement and connection resources. */
$stmt->close();
}
?>
<select name="plantselect" id="plantselect">
<option value="">Select a Plant</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
Select Tool:
<select id="toolselect">
</select>
答案 0 :(得分:3)
这个选择器找不到任何东西:
$("toolselect")
因为没有这样的元素:
<toolselect>
如果您想根据id
属性进行选择,则需要使用#
符号:
$("#toolselect")
此外,您应该删除这些和其他调试echo
语句:
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($conn) . PHP_EOL;
这样的语句可用于调试,但不适用于生产代码。至少,这意味着您正试图将原始文本插入select
元素,这可能只是默默地失败,因为这对元素没有意义。