hi guys,
1. i use this script from w3schools it works with real escape string. But my problem is it is slow displaying the table and i dont know how to check the problem. assuming password and table is correct.
2. can i change the "ajax_demo" value or delete?
reference: http://www.w3schools.com/php/php_ajax_database.asp
Index.php
<html>
<head>
<script>
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","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<!-- <form enctype="multipart/form-data"> -->
<form>
Category: <select name="select_cat" onChange="showUser(this.value)">
<?php
$con = mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("table");
$sql = "SELECT * FROM contacts";
$data = mysql_query($sql,$con);
while($category = mysql_fetch_array($data)){
$catname = $category['firstname'];
$valueid = $category['id'];
echo '<option value="'.$valueid.'">'.$catname.'</option>';
}
?>
</select>
<br />
</form>
<div id="txtHint"><b>Data</b></div>
</body>
</html>
getuser.php
<?php
$q=$_GET["q"];
$con = mysqli_connect('localhost','root','password','table');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
//mysqli_select_db($con,"ajax_demo");
mysqli_select_db($con,"ajax_demo");
//$sql="SELECT * FROM user WHERE id = '".$q."'";
$sql="SELECT * FROM contacts WHERE id = '" . mysql_real_escape_string($q) ."'";
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Phone</th>
<th>Email</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Thanks in advance!