4

Inside a PHP script I have this:

echo <<<EOD

<script type="text/javascript">
document.getElementById('my_element_id').innerHTML='Do stuff';
</script>

EOD;

Can I add PHP inside the JavaScript? Replace the "Do stuff" part with PHP code? If yes, how do I do it?

7
  • 1
    php.net/manual/en/… Commented Oct 3, 2014 at 12:39
  • 1
    Yes you can do it... and before asking try to implement that thing..:) Commented Oct 3, 2014 at 12:40
  • If you add it in using PHP tags it will run at page serving time. If you want it to run at some point during the use of the page (e.g. in response to a click) you will need to use AJAX. Commented Oct 3, 2014 at 12:42
  • You surely can do it... Commented Oct 3, 2014 at 12:42
  • ......d').innerHTML='<?php echo "I am PHP" ?>'; Commented Oct 3, 2014 at 12:42

3 Answers 3

3

First of all, it should be noted that this has nothing to do with javascript. You could have any form of text. Your actual question is how to use a variable inside of a heredoc.

Heredoc is defined as the following:

Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping.

Meaning that since this works:

$name = 'Foo';

echo "My name is $name"; // Using double quotes so variables get expanded

Then this also works:

$name = 'Foo';

echo <<<EOD
    My name is <strong>$name</strong>
EOD;  // Using heredoc so variables get expanded

Essentially meaning that yes, as long as you put your 'Do stuff' content into a variable first. Note that if you use more advanced variables/arrays, it's a good idea to do a $array = json_encode($array) before pasting it into JS code (imagine if $name was The Boss's Wife - then the apostrophe would ruin your JS if you don't encode it).

DEMO

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

Comments

0

Can be done like:

<?php 
$anyphpvariable='foo';
echo <<<EOD 
    <script type="text/javascript">
       document.getElementById('my_element_id').innerHTML=$anyphpvariable;

    </script>
EOD;
?>

Anything from php can be assign to JS and its good practice. But not JS can assign anything to PHP.

4 Comments

That's going to throw a Reference error because foo is not defined as a JavaScript variable.
surround $anyphpvariable with quotes like ...erHTML='$anyphpvariable';
Providing $anyphpvariable contains no new lines or ' characters (which it doesn't in this example, but that isn't a robust solution). @h2ooooooo's answer covers this and is much better.
Yes thats better for sure. why did you not accept @h2ooooooo's answer?
-3

Yes, but I can only get what you want if I place the code after the relevant HTML. You can echo scripts...for example, this will work:

<div id="footer">Hey</div>
<?php 
$myVar = 'Hello';
echo "<SCRIPT>
   document.getElementById('footer').innerHTML = '$myVar';
</SCRIPT>";
?> // You will see Hello in the footer

but this will not work:

<?php 
$myVar = 'Hello';
echo "<SCRIPT>
   document.getElementById('footer').innerHTML = '$myVar';
</SCRIPT>";
?>
<div id="footer">Hey</div>// You will see Hey in the footer

As a side note, these other functions/methods will work too:

$myVar = 'Hi';
echo "<SCRIPT>
    alert('$myVar');
</SCRIPT>";//Alerts 'Hi'

$myVar = 'home.php';
echo "<SCRIPT>
    location = '$myVar';
</SCRIPT>";//Takes you to home.php

$myVar = 'hi';
echo "<SCRIPT>
    myFunction('$myVar');
</SCRIPT>";//calls myFunction with argument $myVar

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.