将var从javascript发送到php文件
它尝试在php中使用Ajax。 编写一段代码,最后将var“Id”发送到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']
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。