 |
พอมีตัวอย่าง autocomplete ที่ ให้โชว์ค่าตามเงื่อนไข select box ด้วย get บ้างไหมครับ |
|
 |
|
|
 |
 |
|
พอมีตัวอย่าง autocomplete ที่ ให้โชว์ค่าตามเงื่อนไข select box ด้วย get บ้างไหมครับ
ตัวอย่าง item=hmtb23 อยากให้ autocomplete select เฉพาะที่มี id = hmtb23 ครับ
ตอนนี้มันดึงมาหมด

code auto
Code (JavaScript)
<script type="text/javascript">
function make_autocom(autoObj,showObj){
var mkAutoObj=autoObj;
var mkSerValObj=showObj;
new Autocomplete(mkAutoObj, function() {
this.setValue = function(id) {
document.getElementById(mkSerValObj).value = id;
}
if ( this.isModified )
this.setValue("");
if ( this.value.length < 1 && this.isNotClick )
return ;
return "data.php?q=" +encodeURIComponent(this.value);
});
}
// การใช้งาน
// make_autocom(" id ของ input ตัวที่ต้องการกำหนด "," id ของ input ตัวที่ต้องการรับค่า");
make_autocom("Search","h_arti_id");
</script>
Code (PHP)
<?php
header("Content-type:text/html; charset=UTF-8");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
// เชื่อมต่อฐานข้อมูล
require("config.php");
ob_start('mb_output_handler');
setlocale(LC_ALL, 'th_TH');
$q = urldecode($_GET["q"]);
//$q= iconv('utf-8', 'tis-620', $_GET['test']);
$pagesize = 10; // จำนวนรายการที่ต้องการแสดง
$table_db="yearbudget"; // ตารางที่ต้องการค้นหา
$find_field="IdBud"; // ฟิลที่ต้องการค้นหา
$sql = "select DISTINCT IdBud from $table_db where locate('$q', $find_field) > 0 order by locate('$q', $find_field), $find_field limit $pagesize";
$results = mysql_query($sql);
while ($row = mysql_fetch_array( $results )) {
$id = $row["IdBud"]; // ฟิลที่ต้องการส่งค่ากลับ
$name =$row["IdBud"]; // ฟิลที่ต้องการแสดงค่า
// ป้องกันเครื่องหมาย '
$name = str_replace("'", "'", $name);
// กำหนดตัวหนาให้กับคำที่มีการพิมพ์
$display_name = preg_replace("/(" . $q . ")/i", "<b>$1</b>", $name);
echo "<li onselect=\"this.setText('$name').setValue('$id');\">$display_name</li>";
}
mysql_close();
?>
Tag : PHP, MySQL
|
ประวัติการแก้ไข 2014-09-25 13:16:35
|
 |
 |
 |
 |
Date :
2014-09-25 13:15:30 |
By :
kanokpong |
View :
1123 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
|