我有一個相當的腳本,將項目添加到表中。我需要從基於通過JavaScript傳遞的UPC的MySQL數據庫中提取信息。使用document.write()在JavaScript中使用PHP;
我試過:document.write("<?php echo '375'; ?>");
只是爲了看看它是否可行,一旦腳本到達那一行,頁面刷新並顯示一個空白頁面。
完整的JavaScript低於:
//setup before functions
var field = document.getElementById("UPC");
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
if (field.value.length != 0) {
document.getElementById("noScan").className="hidden";
document.getElementById("checkout").className="";
document.getElementById("void").className="";
var upc=document.getElementById("UPC").value;
var price = document.write("<?php echo '375'; ?>");
var weight = parseInt(document.getElementById("weight").value);
var table=document.getElementById("ScannedItems");
var total = weight * price;
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="Example Description";
cell3.innerHTML = "$" + price.toFixed(2);
cell4.innerHTML = weight + " lbs";
cell5.innerHTML = "$" + total.toFixed(2);
cell5.setAttribute('data-total', total); // caches the total into data
cell6.innerHTML="<a class='add'><span class='glyphicon glyphicon-plus' style='padding-right:15px;'></span></a><a class='delete'><span class='glyphicon glyphicon-minus'></span></a>";
field.value ='';
var total = cell5.getAttribute('data-total');
var salesTax = Math.round(((total/100) * 8.25)*100)/100;
var totalAmount = (total*1) + (salesTax * 1);
document.getElementById('displaysubtotal').innerHTML="$" + (Math.floor(total * 100)/100).toFixed(2);
document.getElementById('displaytax').innerHTML="$" + salesTax;
document.getElementById('displaytotal').innerHTML="$" + totalAmount;
}
}
// Duplicate a scanned item
var $table = $('#ScannedItems');
$('#ScannedItems').on('click', '.add', function() {
var $tr = $(this).closest('tr').clone();
$table.append($tr);
});
// Remove a line item
var $table = $('#ScannedItems');
$('#ScannedItems').on('click', '.delete', function() {
var $tr = $(this).closest('tr').remove();
});
我必須弄清楚如何從我的數據庫獲取信息的這個項目,或者是要失敗的。
好的,所以我重新創建了我的javascript以包含一個ajax現在我如何從數據庫中將拉取的數據返回到javascript中以將其顯示在表中? –
無論您在您的回聲中PHP代碼將是s作爲AJAX迴應。我建議你閱讀關於AJAX的教程(相信我,這是值得的...你將再次使用AJAX)。對於這種用法,我建議您以JSON格式輸出PHP的響應。然後它將作爲JS對象/數組接收,並作爲JS對象/數組,您可以通過循環來創建表中的行。 – Travesty3