0

I am trying to perform a simple AJAX request from a table but when I var dump the result comes back as undefined?

Here is the JS:

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 (this.readyState==4 && this.status==200) {
      document.getElementById("txtHint").innerHTML=this.responseText;
    }
  }
  xmlhttp.open("GET","actions/teamData.php?q="+str,true);
  xmlhttp.send();
}

I have created a form that select strictly against ID 1 because when trying to do this dynamically with Php it was doing the same so I have set it to 1 to be sure for now:

<option value="1">' . $row['name'] . '</option>

this is the php doing the request:

$q = intval($_GET['q']);

var_dump($_GET);

$sql = "SELECT * FROM team WHERE id = ' " . $q . " ' ";
$result = mysqli_query($conn,$sql);

echo "<table>
<tr>
<th>Team name</th>
</tr>";

while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['name'] . "</td>";
    echo "</tr>";
}
echo "</table>";

mysqli_close($conn);

I have a valid connection which has been tested. But the result comes back as:

array(1) { ["q"]=> string(9) "undefined" }
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
PhpDude
  • 1,542
  • 2
  • 18
  • 33
  • print your query and check what are you getting? – prakash tank Feb 22 '17 at 09:05
  • What do you send as the variable str? – Julian Schmuckli Feb 22 '17 at 09:06
  • 1
    Here `id = ' " . $q . " ' ";` remove the spaces around the number so `id = '" . $q . "' ";` – RiggsFolly Feb 22 '17 at 09:06
  • Or better still as its a number `WHERE id = $q";` – RiggsFolly Feb 22 '17 at 09:07
  • @RiggsFolly its still returning as 'undefined' - I copied what you wrote to be double sure. – PhpDude Feb 22 '17 at 09:07
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Feb 22 '17 at 09:07
  • @RiggsFolly im just toying around with this, this isn't going anywhere near production – PhpDude Feb 22 '17 at 09:08
  • 2
    Bad habits learned while playing will get to production. – RiggsFolly Feb 22 '17 at 09:09
  • @RiggsFolly I agree on that point, but I promise it isnt. I just want to see how Ajax plays before I go ahead and scope it properly – PhpDude Feb 22 '17 at 09:09
  • Are you sure that `str` the parameter has actually picked up the `1` the code that calls `showUser(str) {` is not in your example – RiggsFolly Feb 22 '17 at 09:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/136326/discussion-between-phpdude-and-riggsfolly). – PhpDude Feb 22 '17 at 09:13
  • Are you sure you have a `name` column in the `team` table? – RiggsFolly Feb 22 '17 at 09:14
  • @RiggsFolly im such a dumb F - too early in the morning for me. I had `name="teamData" onchange="showUser(this.value)"` on the form and not in the select - face palm – PhpDude Feb 22 '17 at 09:14
  • 1
    Haha, we have all done it. Better luck for the rest of the day – RiggsFolly Feb 22 '17 at 09:15

1 Answers1

3
array(1) { ["q"]=> string(9) "undefined" }

You have got it because javascript variable "str" is undefined. Just check it and send a valid ID.

Start writing you function in a next way:

function send_xhr(str) {
  str = str || '';
  if ( !str ) { // str is empty
    // Do something
  }

  // str is not empty
}