开发者

Heredoc escaping PHP variables in JavaScript

开发者 https://www.devze.com 2023-02-16 23:29 出处:网络
I have the following code: <?php if ($zweck == \"buchhaltung\") { echo <<<EOF <script type=\"text/javascript\">

I have the following code:

<?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;
    };
?>

... which does not seem to render properly. Can't we use PHP variables in heredoc in JavaScript code like I use it on the second last line?

On the last line I use " ' " around the PHP variable $tb. Is this syntax correct?

The following code is inside the heredoc as JavaScript code:

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

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

var currentTime = new Date();


Variable expansion is performed in heredoc strings, so that's not the problem. The code you give should work fine; if it does not, maybe something else is amiss? What exactly do you mean "does not render properly"?


To get you idea:

<?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?>',...

There isn't any need to escape anything: Just separate your JavaScript code from PHP.

Use each language in its native way.

0

精彩评论

暂无评论...
验证码 换一张
取 消