Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.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_Jquery_Ajax - Fatal编程技术网

Php Ajax没有传输正确的参数

Php Ajax没有传输正确的参数,php,jquery,ajax,Php,Jquery,Ajax,我试图发出一个ajax请求,但是我想要发送的参数没有被正常传输。所以我有 user.php <script> function showUser(str) { if (str=="") { document.getElementById("txtHint").innerHTML=""; return; } if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera,

我试图发出一个ajax请求,但是我想要发送的参数没有被正常传输。所以我有 user.php

<script>
function showUser(str) {

  if (str=="") {
    document.getElementById("txtHint").innerHTML="";
    return;
  } 
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else { // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
  document.write(str);
  xmlhttp.open("GET","getuser.php?q="+str,true);
  xmlhttp.send();
}
</script>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">John Smith</option>
<option value="2">Lois Griffin</option>
<option value="3">Joseph Swanson</option>
<option value="4">Glenn Quagmire</option>
</select>
</form>
我每次都得到
int 0

问题出在哪里?

这是因为当您正在访问页面时,查询字符串中没有类似于
q
的内容。加-

if (!empty($_GET['q'])) {
  $q = intval($_GET['q']);
  var_dump($q);
}

现在,它将仅在url中获取参数时显示。

在指定为intval之前,检查该值是否为数字。以下是来自:

返回值

成功时为var的整数值,失败时为0。空数组 返回0,非空数组返回1

最大值取决于系统。32位系统的最大 有符号整数范围为-2147483648到2147483647。比如说 这样的系统intval('1000000000000')将返回2147483647。这个 64位系统的最大有符号整数值为 9223372036854775807

字符串很可能返回0,尽管这取决于 字符串最左边的字符。整数转换的一般规则 申请

我建议使用JQuery代替AJAX,这在您学习时更容易掌握

//add JQuery//
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
function showUser(str) {
    $(function() {
        $.ajax({
            url: "/getuser.php", //assumes file is in root dir//
            type: "post",
            data: "q="+str,
            processData: false,
            success: function(response) {
                alert ("processed"); //alerts that PHP processed the request//
                $("#txtHint").html(response); //inserts echoed data into div//
            }
        });
    });
}
</script>
这至少会告诉你他们在说话。但是它在屏幕上不可见,您需要在开发者工具->网络->响应中查看(但也应该在
#txtHint
中)。如果你得到一个响应,请删除该响应并正常尝试

编辑:

另外,请检查开发人员工具中的“网络”选项卡,以确保请求已发送。

请尝试
$q=$\u GET['q'];echo$q
如果与xmlhttprequest结合使用,它会工作得很好,问题是您试图正常访问页面(
getUser.php
),因此没有定义。我更改了它,得到了
未定义变量:q
您可能在该页面的其他地方使用了
$q
。添加该检查,它将被修复。如果我按您的方式执行,则得到
null
。我真的不明白问题出在哪里。很可能变量是空的,我会修改我的答案。现在我得到
string'empty'(length=5)
为什么它不起作用?奇怪的是,我在这一行
上得到了
未定义的索引:q
if(是数字($\u get['q']){
javascript中的值分配不正确,或者PHP没有收到
$\u GET
请求。可以尝试取出
document.write(str);
//add JQuery//
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
function showUser(str) {
    $(function() {
        $.ajax({
            url: "/getuser.php", //assumes file is in root dir//
            type: "post",
            data: "q="+str,
            processData: false,
            success: function(response) {
                alert ("processed"); //alerts that PHP processed the request//
                $("#txtHint").html(response); //inserts echoed data into div//
            }
        });
    });
}
</script>
echo $_POST['q'];
exit;