普通网友 2016-03-10 19:43
浏览 54
已采纳

使用AJAX提交值后,PHP返回$ _GET []值未定义

I have a javascript function that creates an XMLHttpRequest object or ActiveObject based on the browser being used living in a file called ajax.js

HTML: user.php

 <ul id="list-of-developers">
      <li class="list-title"><strong data-new-link="true">DEVELOPERS</strong>
      </li>                       
 </ul>

JAVASCRIPT: ajax.js

function ajaxObj2(meth, url, contype){
var xhttp;
if (window.XMLHttpRequest) 
{
    xhttp = new XMLHttpRequest();
} 
else 
{        
    xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open(meth, url, true);
xhttp.setRequestHeader("Content-type",contype);
xhttp.setRequestHeader("Access-Control-Allow-Origin", "http://localhost:9000/");

return xhttp;
}
function ajaxReturn(x){
    if(x.readyState==4 && x.status ==200)
    {
        return true;
    } 
}

In my user.php file which contains my html and the script tag including the ajax.js file in the head of the document.

Here is how the javascript connects with PHP (NB: This tag is located at the bottom of the document outside the body tag)

JAVASCRIPT: user.php (inline)

  <script>
    window.onload = function()
    {

        var u = "<?= $u ?>";

        var r = "<?= $user_role ?>";

        var x = ajaxObj2('GET', 'retrieveDevelopers.php','text/plain');
        x.onreadystatechange = function()
        {
            alert("H u is: "+u+ " r is: "+r + " RESPONSE: " + x.responseText.trim());
            if(ajaxReturn(x) ==true)
            {
                alert("got here");
                var list_of_devs = document.getElementById("list-of-developers");
                console.log(list_of_devs);
                list_of_devs.innerHTML = x.responseText.trim();
            }
        }
        console.log(u);
        x.send("u="+u + "&r="+r);
    }  


</script>

PHP: in retrieveDevelopers.php

<?php 
      $x = $_GET["u"];
 $m = $_GET["r"];

if(isset($_GET["u"]))
{
    include_once("phpincludes/db_connx.php");
    //echo 
    $person= preg_replace('#[^a-z0-9]#i','',$_GET["u"]);
    $person_sql = "SELECT * FROM USERS WHERE USER_ID=(?) AND ACTIVATED=(?)";
    $person_params = array($person, 1);
    $person_options = array("Scrollable" => SQLSRV_CURSOR_CLIENT_BUFFERED);
    $person_result = sqlsrv_query($db_connx, $person_sql,$person_params, $person_options);
    $person_state = sqlsrv_num_rows($person_result);

    if($person_state < 1)
    {
        echo "Failed to verify user";
        exit();
    }
    else 
    {
        echo("It Worked");
        exit();
    }

}

echo "failed";
exit();
?>

I get errors "Notice: Undefined index: u in line 9" and "Notice: Undefined index: r in 10"

I don't understand why this is. The database connects without issue. These files are all in the same folder except for the database connection script

Filesystem:

  • /retrieveDevelopers.php
  • /user.php
  • /phpincludes.php/db_connx.php
  • /script/ajax.js

System Info

Apache Version:2.4.17
PHP Version: 5.6.15 
MICROSOFT SQL SERVER 2012
Localhost: localhost:9000
  • 写回答

1条回答 默认 最新

  • doulao3905 2016-03-10 19:58
    关注

    You are not sending the var u and r try something like this

    xhttp.open("GET", "retrieveDevelopers.php", true);
    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhttp.send("u=value&u=value");
    

    or

    xhttp.open("GET", "retrieveDevelopers.php?u=value&r=value", true);
    xhttp.send();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 将安全信息用到以下对象时发生以下错误:c:dumpstack.log.tmp 另一个程序正在使用此文件,因此无法访问
  • ¥15 速度位置规划实现精确定位的问题
  • ¥15 代码问题:df = pd.read_excel('c:\User\18343\Desktop\wpsdata.xlxs')路径读不到
  • ¥15 为什么视频算法现在全是动作识别?
  • ¥15 编写一段matlab代码
  • ¥15 用Python做岩石类别鉴定软件
  • ¥15 关于调取、提交更新数据库记录的问题
  • ¥15 之前删了盘从下vs2022遇见这个问题 搞了一整天了
  • ¥15 从Freecad中宏下载的DesignSPHysics,出现如下问题是什么原因导致的(语言-python)
  • ¥30 notepad++ 自定义代码补全提示