I have two files here.... index.html and postback.php
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" onchange="showUser(this.value)">
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
postback.php
<?php
$q=$_GET['q'];
$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
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';
$result = mysql_query($sql);//or die('cannot query '.$result);
echo "<table border='1'>
<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>";
}
echo "</table>";
mysql_close($con);
?>
I have a question on this right now. I made this on my own on. If you read between the lines.., I made it similar to auto post back of ASP.net what I want to know now is this... for example I have stored some data in my database let's say Peter Griffin, Lois Griffin, John Wycliffe and Elton John. According in to the logical program of my PHP... the query in my PHP is a wild card select of First Name and Last Name. Now the User input a name.. let's say Elton Wycliffe... It will Release. John Wycliffe and Elton John... My problem here right now is only the First word is accepted to search it... I dunno what could I do with the proceeding words that will also search it... Like this way... Elton is the first word the user has inputted and Wycliffe is the second word. I want every words are being searched in the database so that it will release the suggested outcome. that's what i want to have it right now...( thanks for reading my post... )