Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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
Php 通过AJAX请求不起作用来回显特定值_Php_Arrays_Ajax - Fatal编程技术网

Php 通过AJAX请求不起作用来回显特定值

Php 通过AJAX请求不起作用来回显特定值,php,arrays,ajax,Php,Arrays,Ajax,我对test.php进行了一个ajax调用,甚至在devtools网络中显示该调用是成功的。 问题是,即使在成功拨打电话后,$country仍没有回音 这是我的密码 ajax.php <!DOCTYPE html> <html> <head> <title>Interval</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1

我对test.php进行了一个ajax调用,甚至在devtools
网络中显示该调用是成功的。
问题是,即使在成功拨打电话后,
$country
仍没有回音

这是我的密码

ajax.php

<!DOCTYPE html>
<html>
<head>
    <title>Interval</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script>
$.ajax({url: 'test.php',
         data: {value: 'India'},
         type: 'post',
         success : function(data){
            alert(data);
         }     
});
</script>
</head>
<body>
</body>
</html>
<?php
if(isset($_POST['value']) && $_POST['value'] == 'India'){
     $country = $_POST['value'];
        echo $country;
}
?>

间隔
$.ajax({url:'test.php',
数据:{值:'印度'},
键入:“post”,
成功:功能(数据){
警报(数据);
}     
});
test.php

<!DOCTYPE html>
<html>
<head>
    <title>Interval</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script>
$.ajax({url: 'test.php',
         data: {value: 'India'},
         type: 'post',
         success : function(data){
            alert(data);
         }     
});
</script>
</head>
<body>
</body>
</html>
<?php
if(isset($_POST['value']) && $_POST['value'] == 'India'){
     $country = $_POST['value'];
        echo $country;
}
?>

您需要分离ajax代码并从ajax调用该文件。发生这种情况的原因是,ajax.php返回的是从
开始到
结束的整个页面输出。因此,ajax.php应该如下所示:

<?php
if(isset($_POST['value']) && $_POST['value'] == 'India'){
    $country = $_POST['value'];
    echo $country;
}
?>

然后可以将脚本编辑为

<script>
$.ajax({url: 'ajax.php',
    data: {value: 'India'},
    type: 'post',
    success : function(data){
        alert(data);
    }       
});
</script>

$.ajax({url:'ajax.php',
数据:{值:'印度'},
键入:“post”,
成功:功能(数据){
警报(数据);
}       
});

记住,您使用AJAX,因此不必刷新页面。使javascript AJAX代码调用的PHP成为一个单独的文件,然后
echo$country然后在js代码中从
success function(response){alert($response);}
不工作仍然没有错误,但它也不会回显输出。请查看我编辑的代码。这意味着我们无法向同一页面发出请求?您可以,但它不会达到您在本用例中的目的。仍然无法工作没有错误,但也不会响应输出。我创建了一个聊天室,