|
 |
|
สอบถามเรื่อง ajax กับ select onchange ค่ะ จะทำให้เลือกจากดรอปดาวน์ลิสแล้วให้เอาค่าไปหาค่าใหม่จากเดต้าเบส แล้วเอาไปโชว์ที่ส่วน div ค่ะ ช่วยทีนะคะ ทำไม่เปนจิงจิงงง |
|
 |
|
|
 |
 |
|
โค้ดที่ใช้ ดัดแปลงจากโค้ดของเวปนี้อ่ะค่ะ http://www.w3schools.com/php/php_ajax_database.asp
เพราะต้องการเอ้าพุดคล้ายๆกัน แต่พอเอามาปรับๆให้ตรงกับที่ต้องการ มันก็ไม่รุเรื่องเรยค่ะ กด dropdownlist แล้วไม่มีอะไรเกิดขึ้น
นี่เปนโค้ดส่วนหน้าหลักค่ะ
Code (PHP)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function showProDetail(str)
{
<? $Pname=$_GET['Pname']; ?> //get มาจากลิ้งค่ะ
if (str=="x")
{
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","getdetail.php?Pname=$Pname&Psize="+str,true); ส่งค่า Pname กับ Psize
xmlhttp.send();
}
</script>
</head>
<body>
<? include('onlineshop.inc.php');?>
<form>
<?
$PID = $_GET['PID'];
$sql = "SELECT * FROM product WHERE Product_Id = '$PID'";
$result = mysql_query($sql);
$p = mysql_fetch_array($result);
//$Pname=$p['Product_Name'];
$Ppic=$p['Product_Pict'];
$Pdesc=$p['Product_Des'];
echo "<img align=center src='$Ppic'> <br>";
echo "<div align=center> <b> $Pname </b></div> <br>";
echo "<div align=center> $Pdesc </div> <br>";
echo "<div align='left'> <select name='Psize' id='Psize' "?> onchange='showProDetail(this.value)'<? echo ">"; //จะให้เปี่ยนค่าแล้วเรียกเอแจ็กค่ะ
$sql = "select * from size";
$dbquery = mysql_db_query($dbname, $sql);
$num_rows = mysql_num_rows($dbquery);
$i=0;
print "<option value='x'> Please Select </option>";
while ($i < $num_rows)
{
$resultSize = mysql_fetch_array($dbquery);
$size_id = $resultSize[Size_Id];
$size_name = $resultSize[Size_Name];
print "<option value=$size_id> $size_name </option>";
$i++;
}
echo "</div>";
?>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div> //โชว์ค่าที่ได้จากเอแจ็ก
</body>
</html>
นี่เป็นส่วนพีเอชพีที่ไว้เอาค่าที่ได้มา ไปหาในเดต้าเบสค่ะ
Code (PHP)
<?php
$Pname=$_GET["Pname"];
$Psize=$_GET["Psize"];
include('onlineshop.inc.php');
$sql="SELECT * FROM product WHERE Product_Name = '".$Pname."' AND Size_Id = '".$Psize."'";
$result = mysql_query($sql);
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['Product_Id'] . "</td>";
echo "<td>" . $row['Product_Name'] . "</td>";
echo "<td>" . $row['Size_Id'] . "</td>";
echo "<td>" . $row['Product_Prices'] . "</td>";
echo "<td>" . $row['Product_Quan'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
ช่วยดูหน่อยนะคะ งงมากจิงจิงงง
Tag : PHP, MySQL, Ajax

|
|
 |
 |
 |
 |
Date :
2012-03-11 00:00:04 |
By :
gellarjung |
View :
2859 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
|
|

|
Load balance : Server 01
|