-2

我有一个将项目添加到表格中的脚本。我需要根据通过 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();
    });

我必须弄清楚如何从我的数据库中获取该项目的信息,否则它将失败。

4

3 回答 3

2

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

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

于 2013-10-01T01:06:00.587 回答
2

Javascript 在客户端执行,PHP 在服务器端执行。所以 PHP 在 JS 启动之前执行完毕。

因此,为了获取新数据,您需要发起对服务器的调用。您可以通过使用所需结果刷新页面或创建AJAX 调用来完成此操作。

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

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

于 2013-10-01T01:06:33.303 回答
-2

您可以在 javascript 中完美地使用 php,因为 php 在 javascript 到达浏览器之前执行。因此,浏览器接收到执行了构建 javascript 语句的 php 结果的页面......浏览器不知道 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 中,您不能使用超过一行!!!!

于 2015-08-15T16:06:19.390 回答