Извлечение данных из базы данных - неопределенная проблема индекса (проблема, связанная с ajax) - PullRequest
1 голос
/ 13 июня 2019

Я следовал руководству по w3schools, но мне не удается заставить скрипт возвращать нужные мне данные, когда я выбираю пользователя из выпадающего меню.Я просто получаю неопределенный индекс: userdrop Вот скрипт (изменил GET с помощью POST, возможно, это то, что я испортил)

<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>
";

1 Ответ

0 голосов
/ 13 июня 2019
function UserInfo(str) {    //method starts with str as an argument
  var string = str;         //str is assigned to string.   
 var http = new XMLHttpRequest();  // create new HttpRequest instance 
 var url = "testing.php";          //the script to call to post data
 var params = 'id=string';          

 http.open("POST", url, true);

 //Send the proper header information along with the request
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

 // Call a function when the state changes.
 http.onreadystatechange = function() {
    if(http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
    }
 }
 http.send(params);
 }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...