好所以我有一個java腳本,當輸入字段停止輸入操作時觸發ajax調用。JavaScript Ajax到PHP,然後返回到Javascript
//setup before functions
var field = document.getElementById("UPC");
var table=document.getElementById("ScannedItems");
var typingTimer; //timer identifier
var doneTypingInterval = 1000; //time in ms, 1 seconds
//on keyup, start the countdown
$('#UPC').keyup(function(){
clearTimeout(typingTimer);
typingTimer = setTimeout(doneTyping, doneTypingInterval);
});
//on keydown, clear the countdown
$('#UPC').keydown(function(){
clearTimeout(typingTimer);
});
function doneTyping() {
//user is "finished typing," do something
var upc=document.getElementById("UPC").value;
document.getElementById("noScan").className="hidden";
document.getElementById("checkout").className="";
document.getElementById("void").className="";
var dataString = 'upc='+ upc;
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "assets/PagePHP/pos/scan.php",
data: dataString,
success: function() {
var row=table.insertRow(-1);
var cell1=row.insertCell(0);
var cell2=row.insertCell(1);
var cell3=row.insertCell(2);
var cell4=row.insertCell(3);
var cell5=row.insertCell(4);
var cell6=row.insertCell(5);
cell1.innerHTML =upc;
cell2.innerHTML ="Description";
cell3.innerHTML ="PRICE";
cell4.innerHTML ="QTY";
cell5.innerHTML ="TOTAL";
cell6.innerHTML ="ACTION";
field.value ='';
}
});
return false;
}
ajax將UPC輸入到表單中,並使用它獲取該特定項目的描述和價格。我需要知道如何將這些信息放回到Java Script調用中來創建表中的行。從PHP中的項目需要回到到下面的行Java腳本:(從上面的腳本拉)
cell1.innerHTML =upc;
cell2.innerHTML ="Description";
cell3.innerHTML ="PRICE";
cell4.innerHTML ="QTY";
我的PHP是短期和簡單,看起來像這樣:
$result = mysqli_query($con,"SELECT * FROM inventory WHERE item_upc='$_POST[upc]'");
while($row = mysqli_fetch_array($result))
{
echo $row['item_upc'];
echo $row['item_description'];
echo $row['item_price'];
}
這一切都必須在不刷新頁面的情況下完成。我GOOGLE瞭如何做到這一點,但不能得到符合我的情況的結果。
從PHP返回什麼?嘗試更新您的成功函數並記錄結果以查看:success:function(data){console.log(data); } –