1

I followed the guide on w3schools but I can't manage to make the script return the data I want when I choose a user from the dropdown menu. I just get undefined index: userdrop Here is the script (changed the GET with POST, maybe that's what I messed up)

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

The dropdown menu:

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

And the php code which doesn't provide me with any results due to undefined index

$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>
";
Dharman
  • 21,838
  • 18
  • 57
  • 107
Mitsos
  • 13
  • 3
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Dharman Jun 12 '19 at 23:52
  • why you changing GET with POST? – Mohit Kumar Jun 12 '19 at 23:56
  • @MohitKumar I always think that POST is safer than GET.. – Mitsos Jun 12 '19 at 23:57
  • My duplicate might not help you because, you simple have the problem with your AJAX, not your PHP per se. Check out how to POST data with AJAX: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send#Example_POST – Dharman Jun 13 '19 at 00:15
  • Better duplicate: [Send POST data using XMLHttpRequest](https://stackoverflow.com/questions/9713058/send-post-data-using-xmlhttprequest) – Dharman Jun 13 '19 at 00:17

1 Answers1

0
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);
 }
Mohit Kumar
  • 902
  • 2
  • 6
  • 18
  • Don't tell the user to change code to your liking, especially if the OP's code is better than yours. – Dharman Jun 13 '19 at 00:10
  • I see an improvement in your answer, however there are still problems with it. e.g. `'id=string'`. You really need to explain your answer and the changes which you are suggesting. – Dharman Jun 13 '19 at 18:48
  • @mitsos can you achieve with this answer? – Mohit Kumar Jun 17 '19 at 00:27
  • @MohitKumar I did something similar a few days ago (before I check out here) but it is pretty much the same with your answer :) So thank you! – Mitsos Jun 18 '19 at 21:42