Fw: [請益] 用Jquery UI的自動完成撈mysql的資料

作者: Artlanis (媽~妳看他啦!)   2015-11-08 16:27:21
※ [本文轉錄自 PHP 看板 #1MFmTCyw ]
作者: Artlanis (Artlanis) 看板: PHP
標題: [請益] 用Jquery UI的自動完成撈mysql的資料
時間: Sun Nov 8 16:26:47 2015
抱歉,我不知道這個問題該在php版還是AJAX版問。
小弟在寫進銷庫,想做一個自動完成的功能。
照網路上教學,使用Jquery UI。
主要是想在使用者填寫訂單時,程式可以自動去撈產品的資料庫。
分成表單端跟後台端。
[表單端]
<form method="POST">
產品編號:<input type="text" id="auto1" name="request_serial" value="">
</form>
<script language="JavaScript">
$("#auto1").autocomplete({
source: "Autocomplete.php",
minLength: 1
});
</script>
[後台端]
<?php
$sql = "SELECT * FROM product WHERE product_serial LIKE
'%".$_POST['request_serial']."%'";
$result = mysql_query($sql, $link_ID);
$arr = Array();
for($i=0; $i< $row=mysql_fetch_array($result);){
$arr[] = $row['product_serial'];
}
echo json_encode($arr);
?>
結果:
自動完成功能無動作。
做了以下測試:
1. 將$sql改為"SELECT * FROM product WHERE product_serial";
可以正常動作,表示表單端的程式碼沒有問題。
2. 直接開啟Autocomplete.php(後台端)
出現 Notice: Undefined index: request_serial。
我懷疑是在後台端要$_POST['request_serial']時出錯,因為使用者還沒有輸入關鍵字,
JS以為是空值就出錯停止了。
以下為完整程式碼
表單端
https://goo.gl/VdcOEe
後台端
https://goo.gl/XYf1Oh
使用xampp v5.6.14
請教各位前輩我該如何修正這個問題,是不是有甚麼眉角我沒有注意到的,謝謝大家。
作者: DKMonster (DKMonster)   2015-11-09 04:32:00
用isset來判斷$_post是否存在
作者: Artlanis (媽~妳看他啦!)   2015-11-09 19:44:00
謝謝,問題已經解決了,解法請到php版看看。

Links booklink

Contact Us: admin [ a t ] ucptt.com