在 jQuery 脚本中使用 Php 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17426982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:51:51  来源:igfitidea点击:

Using Php value in jQuery script

phpjqueryhtmlvariables

提问by Iain Simpson

how would I go about using a php value in jQuery ?, what I am doing is pulling the vat rate from the database using php as follows and storing in $vatrate :

我将如何在 jQuery 中使用 php 值?,我正在做的是使用 php 从数据库中提取增值税率,如下所示并存储在 $vatrate 中:

$sqlv = <<<SQL
SELECT *
FROM   `vatrate`
WHERE  id='1'
SQL;
if(!$resultv = $db->query($sqlv)){
  die('There was an error running the query [' . $db->error . ']');
}
while($rowv = $resultv->fetch_assoc()){
    $vatrate =  $rowv['vatrate'];
} ?>

Then I have my jQuery script that adds all of the line totals together and puts it into a span.

然后我有我的 jQuery 脚本,它将所有行总数加在一起并将其放入一个跨度中。

<script>
$(document).ready(function() {
    $('input').on('keyup', function() {
        var rawValue, grandtotal = 0;
        $('span[id^="linetotal"]').each(function(i, elem) {
            rawValue = $.trim($(this).text());
            if (rawValue == '') rawValue = 0;
            grandtotal += parseFloat(rawValue);
        });
        $('#grandtotal').text(grandtotal);
    });
});
</script>

But now im not sure how to reference the $vatrate value declared in php in jQuery, so that I can get the price + vat. VAT is sales tax for anyone not in the Uk :) .

但是现在我不确定如何在 jQuery 中引用 php 中声明的 $vatrate 值,以便我可以获得价格 + 增值税。增值税是任何不在英国的人的销售税 :) 。

回答by Alessandro Minoccheri

In your PHP page into your javascript/jquery code you can do somenting like this to assign a PHP variable to your javascript variable (N.B. you can't do the oppisite because PHP is server side and javascript is client side):

在您的 javascript/jquery 代码中的 PHP 页面中,您可以执行这样的操作以将 PHP 变量分配给您的 javascript 变量(注意,您不能这样做,因为 PHP 是服务器端而 javascript 是客户端):

<script>
    var vatrate = '<?php echo($vatrate);?>';
</script>

回答by Half Crazed

Just echo the PHP where you want the value to reside in the script...

只需在您希望值驻留在脚本中的 PHP 处回显...

<?php $bar = "bar"; ?>
<script>
var foo = "<?php echo $bar; ?>";
alert(foo); // bar
</script>

However, you should never rely too heavily on javascript without the proper fallbacks.

但是,如果没有适当的回退,您永远不应该过分依赖 javascript。

回答by MrCode

If you echo the variable into Javascript, you can omit the the quotes and cast it as a float, which will mean the Javascript variable is a float, and will be ready to work with for the calculation. Casting as a float will also prevent any possibility of Cross Site Scripting (XSS).

如果您将变量回显到 Javascript 中,您可以省略引号并将其转换为浮点数,这意味着 Javascript 变量是一个浮点数,并且可以用于计算。转换为浮点数还可以防止跨站点脚本 (XSS) 的任何可能性。

<script>
var vatRate = <?php echo (float)$vatrate; ?>;

$(document).ready(function() {
    $('input').on('keyup', function() {
        var rawValue, grandtotal = 0;
        $('span[id^="linetotal"]').each(function(i, elem) {
            rawValue = $.trim($(this).text());
            if (rawValue == '') rawValue = 0;
            grandtotal += parseFloat(rawValue);
        });

        // add the vat to the grandtotal
        // assuming vatRate is 20 for 20%, not 0.2
        grandtotal += (grandtotal * (vatRate / 100));
        $('#grandtotal').text(grandtotal);
    });
});
</script>