猿问

从数据库中检索数据-未定义索引问题(ajax相关问题)

我遵循了 w3schools 上的指南,但是当我从下拉菜单中选择一个用户时,我无法让脚本返回我想要的数据。我只是得到未定义的索引:userdrop 这是脚本(用 POST 更改了 GET,也许这就是我搞砸的)


<script>

function UserInfo(str) {

    if (str == "") {

        document.getElementById("txtHint").innerHTML = "";

        return;

    } else {

        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 (this.readyState == 4 && this.status == 200) {

                document.getElementById("txtHint").innerHTML = this.responseText;

            }

        };

        xmlhttp.open("POST","testing.php",true);

        xmlhttp.send();

    }

}

</script>

下拉菜单:


<form>

<select name="userdrop" onchange="UserInfo(this.value)">

  <option value="">Select a person:</option>

  <option value="1">Peter Griffin</option>

  <option value="2">Lois Griffin</option>

  <option value="3">Joseph Swanson</option>

  <option value="4">Glenn Quagmire</option>

  </select>

</form>

<br>

<div id="txtHint"><b>Person info will be listed here...</b></div>

以及由于未定义索引而没有为我提供任何结果的 php 代码


$row = $MMM->Users(intval($_POST['userdrop']));


echo "<table>

<tr>

<th>ID</th>

<th>Name</th>

<th>City</th>

</tr>

<tr>

<td>".$row['user_id']."</td>

<td>".$row['user_name']."</td>

<td>".$row['user_city']."</td>

</tr>

</table>

";


小唯快跑啊
浏览 145回答 1
1回答

米琪卡哇伊

function UserInfo(str) {&nbsp; &nbsp; //method starts with str as an argument&nbsp; var string = str;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//str is assigned to string.&nbsp; &nbsp;&nbsp;var http = new XMLHttpRequest();&nbsp; // create new HttpRequest instance&nbsp;&nbsp;var url = "testing.php";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //the script to call to post data&nbsp;var params = 'id=string';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;http.open("POST", url, true);&nbsp;//Send the proper header information along with the requesthttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');&nbsp;// Call a function when the state changes.&nbsp;http.onreadystatechange = function() {&nbsp; &nbsp; if(http.readyState == 4 && http.status == 200) {&nbsp; &nbsp; &nbsp; &nbsp; alert(http.responseText);&nbsp; &nbsp; }&nbsp;}&nbsp;http.send(params);&nbsp;}
随时随地看视频慕课网APP
我要回答