|
 |
|
ค้นหาข้อมูลโดยใช้ select option และ ajax เพื่อค้นหาข้อมูลค่ะ |
|
 |
|
|
 |
 |
|
ช่วยแนะนำหน่อยค่ะ อยากจะค้นหาข้อมูลเพื่อออกรายงานตามเดือนและปีค่ะ โดยค้นหาโดยใช้ ajax ค่ะ โดยการเลือกที่ select list สองอันก็คือเลือกจากเดือน และเลือกจากปีค่ะ อยากทราบวิธีการส่งค่า พร้อมกันสองค่าของ ajax จาก select list ค่ะ เพื่อใช้ในการค้นหา
โค้ดแสดงข้อมูลค่ะ
Code (PHP)
<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","repair_showreport.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value,"")">
<option value="">กรุณาเลือกเดือน</option>
<option value="01">มกราคม</option>
<option value="02">กุมภาพันธ์</option>
<option value="03">มีนาคม</option>
<option value="04">เมษายน</option>
<option value="05">พฤษภาคม</option>
<option value="06">มิถุนายน</option>
<option value="07">กรกฎาคม</option>
<option value="08">สิงหาคม</option>
<option value="09">กันยายน</option>
<option value="10">ตุลาคม</option>
<option value="11">พฤศจิกายน</option>
<option value="12">ธันวาคม</option>
</select>
<br>
<select name="users" onchange="showUser(this.value)">
<option value="2554">2554</option>
</select>
</form>
<br />
<div id="txtHint"><b>โชว์ข้อมูล</b></div>
</body>
</html>
และโค้ดในส่วนของการค้นหาค่ะ อยากให้รับค่ามาสองค่า คือ เดือนและปี จาก select list ข้างบนค่ะ
Code (PHP)
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'peter', 'abc123');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ajax_demo", $con);
$sql="SELECT * FROM user WHERE id = '".$q."'";
$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['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);
?>
Tag : PHP, MySQL

|
ประวัติการแก้ไข 2011-10-24 21:24:58
|
 |
 |
 |
 |
Date :
2011-10-24 21:24:27 |
By :
csdorm |
View :
3503 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คุณจะต้องใส่ค่าไปทั้ง 2 ตัวครับ
Code (JavaScript)
<select name="month" onchange="showUser(document.form.month.value,document.form.year.value)">
<select name="years" onchange="showUser(document.form.month.value,document.form.year.value)">
|
 |
 |
 |
 |
Date :
2011-10-25 06:40:25 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากค่ะพี่วิน
|
 |
 |
 |
 |
Date :
2011-10-25 10:50:44 |
By :
csdorm |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าจะใช้ ajax ให้ ศึกษา เรื่อง การส่งค่าผ่านทาง javascript ก่อน ครับ จะได้เข้าใจเรื่อง ajax ได้เร็ว ขึ้น ครับ
|
 |
 |
 |
 |
Date :
2011-10-25 21:40:51 |
By :
popnakub |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|