Asynchronous JavaScript and XML

Home

AJAX Tutorial
Knowledge Developer Database Internet Resource
AJAX Tutorial
1. й AJAX
2. ҧ AJAX
3. ҧ XMLHttpRequest Object
4. 觤Ӣ
5. õͺʹͧҡ
6. onreadystatechange Event
7. AJAX PHP/ASP
8. AJAX Database
9. AJAX XML
 
Internet
PHP
SSI
HTML
AJAX
 

AJAX Database

AJAX öẺѹѺҹ

ҧ AJAX Database

ҧҸԵ fetch ʹȢͧྨҡҹŴ AJAX

ҧ


ʹȺؤŨлҡ

͸Ժµҧ - ҹ MySQL

ҧŢͧҹŨѡɳй

id FirstName LastNam Age Hometown Job
1 Wiwat Pongprai 31 Kanjanaburi Tailor
2 Kamon Pengsa 35 Chiengmai Farmer
3 Amphon Taideang 29 Ayuthaya Teacher
4 Wanphen Duangmenee 35 Supanburi Physician
5 Kitti Mingmongkon 42 Samutprakan Fishermen
6 Nalinee Wattana 29 Patumthani Nurse

͸Ժµҧ - showCustomer() Function

ͼ͡١ dropdown list ѧѹ showCustomer() Ѻûż ѧѹѺѺ¹ onchange event


<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","getuser.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">Wiwat Pongprai </option>
<option value="2">Kamon Pengsa</option>
<option value="3">Amphon Taideang</option>
<option value="4">Wanphen Duangmenee</option>
<option value="5">Kitti Mingmongkon</option>
<option value="6">Nalinee Wattana</option>
</select>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>


ѧѹ showCustomer() ӧҹѧ

  • Ǩͺ ͡١
  • ҧͺਤ XMLHttpRequest
  • ҧûżſѧѹ ͡õͺʹͧҡ¾
  • ͡ҡ
  • ѧࡵ (q) Ѻ URL (Ңͧ dropdown list)

The AJAX Server Page

ྨ·¡ JavaScript PHP getcustomer.php ö¹ ASP

ش getcustomer.php ¡Ѻҹ ͡Ѿ㹵ҧ HTML


<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'wide_learn', 'wide911');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("widebase_learn", $con);

$sql="SELECT * FROM ajax_db_user WHERE id = '".$q."'";

$result = mysql_query($sql);
$row = mysql_fetch_array($result);

echo "<table border='1' width='100%'>";
echo "<tr><td width='100'>Firstname</td><td>". $row['FirstName'] . "</td></tr>";
echo "<tr><td>Lastname</td><td>" . $row['LastName'] . "</td></tr>";
echo "<tr><td>Age</td><td>" . $row['Age'] . "</td></tr>";
echo "<tr><td>Hometown</td><td>" . $row['Hometown'] . "</td></tr>";
echo "<tr><td>Job</td><td>" . $row['Job'] . "</td></tr>";
echo "</table>";

mysql_close($con);
?>


 

  
[an error occurred while processing this directive]