简体   繁体   中英

Advance search through ajax and php

I am trying to do advance search through php and AJAX but i stuck and i don't know where is my mistake. plz guide thanks.........

My PHP code

 <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> <style> table { width: 100%; border-collapse: collapse; } table, td, th { border: 1px solid black; padding: 5px; } th {text-align: left;} </style> </head> <body> <?php $servername = "localhost"; $username = "root"; $password = ""; $conn = mysql_connect($servername, $username, $password); if (!$conn) { die('Could not connect: ' . mysqli_error($conn)); } $in = $_GET['str']; if (!ctype_alnum($in)) { echo "Data Error"; exit; } mysql_select_db('firstdb'); $sql = "select name, id , age, sex from name where name like '%$in%'"; $display = mysql_query($sql, $conn); echo "<table> <tr> <th>ID</th> <th>Name</th> <th>Age</th> <th>Gender</th> </tr>"; while ($row = mysql_fetch_assoc($display)) { echo "<tr>"; echo "<td>" . $row['id'] . "</td>"; echo "<td>" . $row['name'] . "</td>"; echo "<td>" . $row['age'] . "</td>"; echo "<td>" . $row['sex'] . "</td>"; echo "</tr>"; } echo "</table>"; ?> </body> </html> 

My Javascript code and AJAX function

 <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> <script> function user(str){ if(str.length==0){ document.getElementById("userhint").innerHTML = ""; return; } else { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("userhint").innerHTML = xmlhttp.responseText; } } xmlhttp.open("GET", "advanceSearch.php?q="+str, true); xmlhttp.send(); } } </script> </head> <body> <form> <input type="text" onkeyup="user(this.value);" name="username" /> </form> <br /> <div id="userhint"><b>User info will be listed here...</b></div> </body> </html> 

Error occurred

在此处输入图片说明

I am new here plz help

use isset to get no error

but you passing q=

 if(isset($_GET['str'])){ // it will  if(isset($_GET['q'])){

    //all you server code


    }

In your http get request you are sending a parameter called q , but in the server side you are trying to fetch that parameter by the name str . so str is not found.

change the parameter name like this:

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

In your PHP file you are trying to fetch str from the request:

$in=$_GET['str'];

But, in your js you are sending the q

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

Change this to

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

Update:

In your ajax query, add the extra parameter you need:

xmlhttp.open("GET", "advanceSearch.php?str="+str+'&name='+name+'&age='+age true);

Assuming, you know how to get the name and age values from you html.

In your PHP add these parameters to your query.

$sql = "select name, id , age, sex from name where name like '%$in%' AND age = `$age`";

Simple thing, parameter that you declare in your GET Url is not str but q .

so, change to $_GET['q']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM