I don't have any syntax error with this code on my PHP file.. I have only a problem when it comes to output of the search query error messaging...
here's the code
postback.php
<?php
$q=$_GET['q'];
$words=explode(' ',$q);
$con = mysql_connect('localhost', 'root', '')or die('Could not connect: ' . mysql_error());
mysql_select_db("ajax_demo", $con)or die('cannot connect');
//$sql = 'SELECT * FROM `ajax_demo_table` WHERE `FirstName` LIKE CONVERT(_utf8 \'%'.$q.'%\' USING latin1) COLLATE latin1_swedish_ci OR `LastName` LIKE CONVERT(_utf8 \'%'.$q.'%\' USING latin1) COLLATE latin1_swedish_ci';
$q = preg_replace('/\s\s+/', ' ', $q); // remove extra space
for( $i = 0; $i < count($words); ++$i ){
$sql = "SELECT *,CONCAT(FirstName, ' ',LastName) as fullname FROM `ajax_demo_table` WHERE CONCAT(FirstName, ' ',LastName) LIKE CONVERT(_utf8 '%".$words[$i]."%' USING latin1) COLLATE latin1_swedish_ci ";
$result = mysql_query($sql) or die('cannot query '.$result);
echo "<table border='1'>";
//if(mysql_fetch_array($result)>=1){
echo"
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}if(mysql_fetch_array($result)==0){
echo "<tr><td><b>NO<b> similar names with <font color=red><b>".$words[$i].'</b></font></td></tr>';
}
/*else{
echo "<tr><td><b>NO<b> similar names with <font color=red><b>".$words[$i].'</b></font></td></tr>';
}*/
}
//end of while
//}
//}
echo "</table>";
mysql_close($con);
?>
what i want to occur now is when a user inputs a string or character or a number wherein it doesn't exists in database... there will be an error message such as "input query not existing"
to test the file here's the index.html
<html>
<head>
<script type="text/javascript">
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","postback.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<!--<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>-->
<input type="text" name="users" onkeyup="showUser(this.value)">
<input type="reset" value="Clear All">
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>
</form>
</body>
</html>