我已经阅读了该网站上的数十个类似问题,并且在试图理解我的代码有什么问题时遇到了很多麻烦。我正在尝试根据另一个选择框的值动态更新选择框,但由于某种原因,一旦我使用 Ajax 发布到 PHP,似乎无法获取任何类型的响应数据。
JAVASCRIPT:
function toggleHiddenDIV()
{
var dc = document.getElementById("datacenter");
var dcVal = dc.options[dc.selectedIndex].value;
// Check if Datacenter selection has no Value selected
if(dcVal != '')
{
document.getElementById("hidden-options").style.display="block";
$.ajax({
type: "POST",
url: "handler.php",
data: { 'action_type': 'update_inventory_fields', id: dcVal },
success: function(response)
{
alert(response);
}
});
}
else
{
document.getElementById("hidden-options").style.display="none";
}
};
</script>
PHP:
if ($_POST['action_type'] == "update_inventory_fields")
{
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["id"])) { return; }
}
$result = mysql_query("SELECT id, ip, block FROM ipv4 WHERE datacenter = " . $_POST["id"]);
$data = array();
while($row = mysql_fetch_array($result, true))
{
$data[] = $row;
};
return json_encode($data);
}