Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.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
将var从javascript发送到php文件_Javascript_Php_Ajax_Web_Yii2 - Fatal编程技术网

将var从javascript发送到php文件

将var从javascript发送到php文件,javascript,php,ajax,web,yii2,Javascript,Php,Ajax,Web,Yii2,它尝试在php中使用Ajax。 编写一段代码,最后将var“Id”发送到php文件中: xmlhttp.open("GET", "details.php?q="+Id,true); 代码中没有问题,details.php运行但是details.php给我这个错误:注意:第2行的C:\wamp64\www\adv3\advanced\frontend\web\details.php中的未定义索引:Id 这是我在details.php中的第二行: <?php $q = $_GET['Id']

它尝试在php中使用Ajax。 编写一段代码,最后将var“Id”发送到php文件中:

xmlhttp.open("GET", "details.php?q="+Id,true);
代码中没有问题,details.php运行但是details.php给我这个错误:注意:第2行的C:\wamp64\www\adv3\advanced\frontend\web\details.php中的未定义索引:Id

这是我在details.php中的第二行:

<?php
$q = $_GET['Id'];

您会收到一个错误,因为您发送的查询字符串参数是
q
,而不是
Id

xmlhttp.open("GET", "details.php?q="+Id,true);  
                                 ^^
所以你的PHP应该是

$q = $_GET['q'];

你在哪个浏览器上?无论如何;您可以使用JQuery使您的生活更加轻松

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script type="text/javascript">
    (function($) {
        $(document).ready(function(){
            $.ajax({
                url     : "details.php",
                type    : "GET",
                data    : {"Id" : Id },
                success: function (data, textStatus, jqXHR){
                },

                error: function (jqXHR, textStatus, errorThrown) {
                    console.log('The following error occurred: ' + textStatus, errorThrown);
                }
            });
        });
    })(jQuery);
</script>

(函数($){
$(文档).ready(函数(){
$.ajax({
url:“details.php”,
键入:“获取”,
数据:{“Id”:Id},
成功:函数(数据、文本状态、jqXHR){
},
错误:函数(jqXHR、textStatus、errorshown){
log('发生以下错误:'+textStatus,errorshown);
}
});
});
})(jQuery);

您正在将查询字符串作为q传递。所以使用$q=$\u GET['q'];而不是Id。