hello i am trying implement a multi search query using an example i found online,

http://www.w3schools.com/php/php_ajax_database.asp

although the example on this page is a single query and makes use of a select menu, i have edited the code to make use of it using a textfield, and it works perfectly,
now am trying to make this one query to function as a multiquery form. but i keep gettin an exception

Notice: Undefined index: q in C:\xampp\htdocs\transportapp\getuser.php on line 2

this is the code that works as a single query

html file :

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

</script>

<form>

  <input name="users" type="text" onKeyUp="showUser(this.value)">

</form>

php file :

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



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

mysqli_select_db($con,"wcp");
$sql="SELECT * FROM user WHERE name = '".$q."'";
$result = mysqli_query($con,$sql);

and this what i have tried doin wen trying to convert to a multiquery

html file :

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

        xmlhttp.send();
    }
}
</script>

<form>

  <input name="users" type="text" onKeyUp="showUser(this.value)">
    <input name="user" type="text" onKeyUp="showUser(this.value)">
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>

php file :

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



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

mysqli_select_db($con,"wcp");
$sql="SELECT * FROM user WHERE name = '".$q."' and username = '".$k."'";
$result = mysqli_query($con,$sql);

and then i get this error Notice: Undefined index: q in C:\xampp\htdocs\transportapp\getuser.php on line 2

i am confused on why am gettin this error. my aim is to pass multiple values via ajax request and query my database

If you want to pass multiple values you can do it this way:

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

In this case you will always have both q and k elements in the $_GET array. But it is a question of what you really intend to do. Maybe some other approach is more appropriate.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.