Masroor_Shah Masroor_Shah - 1 year ago 131
Javascript Question

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>
<meta charset="utf-8">
<title>Untitled Document</title>
table {
width: 100%;
border-collapse: collapse;

table, td, th {
border: 1px solid black;
padding: 5px;

th {text-align: left;}

$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";
$sql = "select name, id , age, sex from name where name like '%$in%'";
$display = mysql_query($sql, $conn);
echo "<table>
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>";

My Javascript code and AJAX function

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

Error occurred

enter image description here

I am new here plz help

Answer Source

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

so, change to $_GET['q']

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download