|
 |
|
ช่วยดู Ajax ทีครับเลือก listbox แล้วไม่ขึ้นข้อมูลแสดง |
|
 |
|
|
 |
 |
|
พอลองเลือกรายชื่อจาก listbox แล้วมันไม่ขึ้นตารางแสดงรายละเอียดขึ้นมาครับ ฐานข้อมูลก็สร้างแล้วนะครับ
ลองทำตามนี้ครับ http://www.w3schools.com/php/php_ajax_database.asp
ajaxdemo.html
Code (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>
<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>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
getuser.php
Code (PHP)
<?php
$q=intval($_GET['q']);
$host = "localhost";
$user = "root";
$passwd = "root";
$db = "ajaxdemo";
$con = mysqli_connect($host,$user,$passwd);
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($db);
$sql="SELECT * FROM user WHERE id = '".$q."' ";
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</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['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Tag : PHP, MySQL, HTML/CSS, JavaScript, Ajax, jQuery

|
ประวัติการแก้ไข 2014-03-29 15:45:41
|
 |
 |
 |
 |
Date :
2014-03-29 15:45:11 |
By :
littlebeer |
View :
892 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
js มี error ไหม
php มี error ไหม
เปิดหน้า getuser โดยตรง แล้วผลออกอย่างที่ต้องการไหม
ตรวจสอบพวกนี้ดูหรือยังครับ
|
 |
 |
 |
 |
Date :
2014-03-29 18:09:56 |
By :
natt_han |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองแบบ jquery ดูครับ
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("select[name='users']").change(function(){
$.get("getuser.php",{q:$("option:selected",this).val()} , function(data){
$("div#txtHint").html(data);
});
});
});
</script>
</head>
<body>
<form>
<select name="users" >
<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>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
|
 |
 |
 |
 |
Date :
2014-03-29 22:37:21 |
By :
sakuraei |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 01
|