0

I want to be able to write a few mysql queries with the returned value(s) of my radio button(s). Here is the code I have

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

                <?php include 'db_connector.php';

                $result = mysqli_query($con,"SELECT * FROM os_scope");

                while($row = mysqli_fetch_array($result)) {

                $row['name'];

                echo "<li><div class='toggle-btn-grp cssonly'>

                        <div><form><input type='radio' name='os' value=".$row['name']." id='myRadio' onchange='showUser(this.value)'>

                        <label class='toggle-btn'><div class='title'>".$row['name']."</form></div></label></div></div></li>";

            }

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

</body>
</html>

Here is my php file getuser.php

  <?php
  $q = intval($_GET['q']);

  $con = mysqli_connect('localhost','root','abc123','mydb');
  if (!$con) {
  die('Could not connect: ' . mysqli_error($con));
  }

  mysqli_select_db($con,"ajax_demo");
  $sql="SELECT * FROM os ";
  $result = mysqli_query($con,$sql);

  echo "<table border='1'>
  <tr>
  <th>ID</th>
  <th>Name</th>
  <th>Scope</th>
  <th>Client</th>
  <th>Supplier</th>
  </tr>";

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

  mysqli_close($con);
  ?>

However, when I run the code I keep getting this error: Notice: Undefined index: q Which means q is not been sent to the php file, any ideas how I can fix this? I would really appreciate all the help I can get. Sorry if this is a dumb question but am really new at this, so I would really appreciate the help. Thanks.

1 Answer 1

1

Change

xmlhttp.open("GET","getuser.php?=q"+str,true);

to

xmlhttp.open("GET","getuser.php?q="+str,true);

The q and = are the wrong way around ;)

Sign up to request clarification or add additional context in comments.

11 Comments

Hi thanks for replying, I've made the changes you suggested, but am still getting the error ** Notice: Undefined index: q ** I get the same error when i run the php file alone as well.
Are you including a value for q in the query string? E.g. in your browser access getuser.php?q=123
Also, in your example code, you have a closing php tag after mysqli_close but then some more PHP. This will also cause unexpected behaviour... If the additional PHP after the closing tag is meant to be there, you should remove the closing PHP tag after mysqli_close.
@joel harkes I tried the following example could you help me to point out where am going wrong?
Hi @Annie - I think you posted on the wrong answer! Did you mean to post on this one? :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.