1

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();
6
  • 1
    Heredoc in not used nowadays.. Try to do it some other way.. If u want to clear this problem , Can you give the full block of code if u need to find the exact problem Commented Mar 16, 2011 at 11:45
  • 2
    @kvijayhari: That's not true. If you want to inject some JS heredoc is the most convenient way. Commented Mar 16, 2011 at 11:48
  • @kvijayhari: It is not depreciated though... Commented Mar 16, 2011 at 11:49
  • @Jon in fact, most convenient way to write JS is pure and clean JS :) Commented Mar 16, 2011 at 11:50
  • What do you recommend than. My code block is huge and it works without the if-conditonal in php with heredoc. My problem is that i want to echo different javascript based on a php conditional. AND i would like to prevent to escape the whole javascript code since i use a lot of " and ' Commented Mar 16, 2011 at 11:53

2 Answers 2

1

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"?

Sign up to request clarification or add additional context in comments.

5 Comments

it does not output the variables. The javascript code is fine ... if i delete the php code before the javascript ... it outputs the data fine. i thought maybe the $tb is not outputed because it is insidea a ', this would be prevented in usual php-code too.
try adding curly braces. Will make it cleaner to read as well. php?tb={$tb}. And make sure $tb actually has a value
@Email: "if i delete the php code before the javascript ... it outputs the data fine" -- doesn't that indicate that the problem is something with that PHP code, and not with the heredoc?
thanks for the hint. does php treat the following javascript code inside heredoc natively correct? $(el).datepicker({dateFormat:'dd.mm.yy'});
@Email: It does. You can check that yourself by trying and viewing the resulting HTML delivered to your browser.
1

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.

2 Comments

Thanks for that. Actually this was my original way to do but i made a mistake with closing the if: <?php } ?> after the conditional with an additonal ;. thanks for putting me on the right path again +1. i would like to chose the answer which makes the heredoc works since my question was about this though to be fair ... if it will not be solved in 30 min - i will check yours. thanks
@Email to make heredoc work you have joust to check resulting syntax. But there is not single reason to use heredoc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.