2013-10-01 30 views
-2

我有一個相當的腳本,將項目添加到表中。我需要從基於通過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(); 
    }); 

我必須弄清楚如何從我的數據庫獲取信息的這個項目,或者是要失敗的。

回答

2

Javascript在客戶端執行,PHP在服務器端執行。所以PHP在JS開始之前執行。

因此,爲了獲取新數據,您需要啓動一個到您的服務器的調用。您可以通過使用所需結果刷新頁面或創建AJAX call來完成此操作。

爲了更清楚一點,請仔細看看您給出的例子。在瀏覽器中查看源代碼。它將以document.write("375");的形式出現。這是因爲在將頁面發送到瀏覽器(這是執行JS代碼的位置)之前,PHP會在服務器端將字符串'375'回顯到您的JS代碼中。

PHP可以生成JS代碼,但JS無法生成PHP代碼(通常意義上說)。

+0

好的,所以我重新創建了我的javascript以包含一個ajax現在我如何從數據庫中將拉取的數據返回到javascript中以將其顯示在表中? –

+0

無論您在您的回聲中PHP代碼將是s作爲AJAX迴應。我建議你閱讀關於AJAX的教程(相信我,這是值得的...你將再次使用AJAX)。對於這種用法,我建議您以JSON格式輸出PHP的響應。然後它將作爲JS對象/數組接收,並作爲JS對象/數組,您可以通過循環來創建表中的行。 – Travesty3

2

PHP在服務器生成HTML到客戶端瀏覽器之前在服務器上執行。客戶端上javascript插入的任何PHP代碼都不會運行。

如果您想讓代碼動態地從PHP插入,您可以研究如何使用AJAX調用來運行單獨的PHP服務器端腳本並插入返回的內容。

-2

你可以完美的使用php裏面的javascript,因爲php在javascript到達瀏覽器之前執行。因此,瀏覽器接收到執行php結果的頁面,該結果構建了javascript語句......瀏覽器不知道JavaScript語句是由您或PHP服務器編寫的。

document.write("<?php echo '375'; ?>"); 

嘗試將其更改爲...

document.write("<?php echo("375"); ?>"); 

的 「」 由PHP服務器是第一次看到這麼上面應該工作。

或以防萬一,你可以逃脫「

我也有這樣的一段代碼功能齊全:

frameDoc.document.write sentence 

<?php 
echo("frameDoc.document.write(\""); 
require('secciones/seccion_head2.html'); 
echo("\");"); 
?> 

frameDoc.document.write sentence 

BUT !!!所需的HTML裏面,你不能使用一個以上的線!!!!

+1

Java與JavaScript不是一回事。其他答案已經表示,PHP代碼將首先運行,並且輸出將隨JavaScript代碼一起發送到瀏覽器。通過使用PHP和document.write()來包含HTML是一個不錯的主意。最重要的是:問題是從JavaScript運行PHP,而不是從PHP輸出JavaScript。 –

+0

嗯,我仍然認爲用戶想使用PHP生成JavaScript,但所有其他答案只是看到簡單的答案,並說「你不能」...但他需要的確切的東西是根據他檢測到的一些情況生成不同的JavaScript使用PHP ...順便說一句,以防萬一我編輯我的答案更友好:) –

相關問題