Heredoc在JavaScript中转义PHP变量

问题描述 投票:1回答:2

我有以下代码:

<?php
    if ($zweck == "buchhaltung") {
        echo <<<EOF
        <script type="text/javascript">
            jQuery(document).ready(function() {
            jQuery("#$grid_name").jqGrid({
                url: 'modules/mod_jqgrid/ex_get3.php?tb=$tb'
                .....
        </script>
EOF;
    };
?>

...似乎无法正确呈现。我们不能像在倒数第二行中那样在JavaScript代码中的Heredoc中使用PHP变量吗?

在最后一行,我在PHP变量$ tb周围使用了“'”。这个语法正确吗?

以下代码作为JavaScript代码在heredoc中:

dataInit:function(el){
    $(el).datepicker({dateFormat:'dd.mm.yy'});
},
defaultValue: function(){

// Maybe PHP "thinks" that $(el) is a PHP variable?

var currentTime = new Date();
php javascript heredoc
2个回答
1
投票

可变扩展是在heredoc字符串中执行的,所以这不是问题。您提供的代码应该可以正常工作;如果没有,也许还有其他问题吗?您到底是什么意思“不能正确渲染”?


1
投票

让您知道:

<?php
    if ($zweck == "buchhaltung"){
?>

<script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery("#<?php echo $grid_name ?>").jqGrid({
            url: 'modules/mod_jqgrid/ex_get3.php?tb=<?php echo $tb?>',...

不需要任何转义:只需将JavaScript代码与PHP分开。

以其母语使用每种语言。

© www.soinside.com 2019 - 2024. All rights reserved.