JavaScript 中的 PHP 使用 document.write();.


PHP in JavaScript using document.write();

我有一个将项目添加到表中的脚本。我需要根据通过JavaScript传递的UPC从MySQL数据库中提取信息。

我试了试: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 在客户端执行,PHP 在服务器端执行。所以 PHP 在 JS 启动之前就完成了执行。

因此,为了获取新数据,您需要发起对服务器的调用。为此,可以使用所需的结果刷新页面,也可以创建 AJAX 调用。

为了更清楚,请仔细查看您给出的示例。在浏览器中查看源代码。它将以document.write("375");的形式出现.这是因为 PHP 在将页面发送到浏览器(这是 JS 代码执行的地方)之前,将字符串 '375' 回显到服务器端的 JS 代码中。

PHP 可以生成 JS 代码

,但 JS 不能生成 PHP 代码(通常意义上)。

PHP 在向客户端浏览器提供生成 HTML 之前在服务器上执行。在客户端上由 javascript 插入的任何 PHP 代码都不会运行。

如果要从 PHP 动态插入代码,可以研究如何使用 AJAX 调用来运行单独的 PHP 服务器端脚本并插入返回的内容。

你可以在javascript中完美地使用php,因为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

但是!!在所需的 html 中,您不能使用多行!!!