Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Ajax发布到PHP并读取结果_Php_Javascript_Jquery_Ajax_Post - Fatal编程技术网

使用Ajax发布到PHP并读取结果

使用Ajax发布到PHP并读取结果,php,javascript,jquery,ajax,post,Php,Javascript,Jquery,Ajax,Post,我已经在这个网站上阅读了几十个类似的问题,并且在试图理解我的代码有什么问题时遇到了很多困难。我试图根据另一个选择框的值动态更新选择框,但由于某些原因,在使用Ajax发布到PHP后,似乎无法返回任何类型的响应数据 JAVASCRIPT: function toggleHiddenDIV() { var dc = document.getElementById("datacenter"); var dcVal = dc.options[dc.selectedIndex].value;

我已经在这个网站上阅读了几十个类似的问题,并且在试图理解我的代码有什么问题时遇到了很多困难。我试图根据另一个选择框的值动态更新选择框,但由于某些原因,在使用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>

不要调用
return
(因为您没有返回函数);只需
echo
然后将内容添加到页面上:

echo json_encode($data);

更改为此…无需
返回
,只需
回显
,因为您在函数调用之外

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;
};

echo json_encode($data);
}

如果发布的php代码位于函数内部,则需要使用
echo functionname()


如果php代码不在函数中,那么只需使用
echo json_encode($data)

在PHP中尝试
echo
而不是
return
。(现在我想是因为我看到的代码不比你发布的代码多)你试过-json_decode(file_get_contents)吗php://input'));你把它弄好了吗?
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;
};

echo json_encode($data);
}