-3

Using PHP to generate JavaScript works as expected, and using JavaScript to generate PHP works as expected, however using PHP to generate JavaScript to generate PHP does not seem to work.

Is there a problem with my code which is causing this to fail, or is it some sort of limitation?


<!-- Use JavaScript to generate PHP --> 

<script type='text/javascript'>
    function addComment()
    {
        alert("About to add some content");
        document.write("<?php testFunction(); ?>");
    }
</script>

<!-- Use PHP to generate JavaScript to generate PHP --> 

echo "<script type='text/javascript'>";
    echo "function addComment2()";
    echo "{";
        echo 'alert("About to add some content");';
        echo 'document.write("<?php testFunction(); ?>");';
    echo "}";
echo "</script>";

(testFunction() simply contains echo "Test";)

Eilidh
  • 1,270
  • 1
  • 13
  • 33
  • 1
    Could someone please explain why this is being downvoted? If my question is phrased unclearly I would like to improve it, but tbh it seems like it's just being downvoted because it's an odd question. It's something I need to do and sarcastic comments / downvotes for no apparent reason are not helpful, to me it seems like this question is just being downvoted because it is obscure and rather strange and not because it is unclear or shows lack of research... – Eilidh Jun 04 '14 at 12:35
  • Your "JavaScript to generate PHP" example does not generate PHP. I don't think you've understood the distinction between the two concepts. – Gerald Schneider Jun 04 '14 at 12:36
  • also duplicate of [How to pass variables and data from PHP to JavaScript?](http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) – NullPoiиteя Jun 04 '14 at 12:43

3 Answers3

3

PHP is code executed on the server side. JavaScript is code executed on the client side. They don't know nothing about the other.

You can generated with PHP nearly whatever you want.

You problem is the fourth echo:

    echo 'document.write("<?php testFunction(); ?>");';

You tell echo to print this string:

document.write("<?php testFunction(); ?>");

This string is sent to the client (and browser). The browser (the javascript engines) runs this code and results in an output like this:

<?php testFunction(); ?>

We have already left the php interpreter on your server, because we are already on the client. The client can't to anything with this.

If you replace your line with this:

    echo 'document.write("', testFunction() ,'");';

your code will executed as expected.

But for this you have change your testFunction. Just replace

function testFunction() {
    return "Test";
};

and the output to the server will be

    document.write("Test");
tjati
  • 5,761
  • 4
  • 41
  • 56
  • Thank you very much, I really appreciate you taking the time to explain it properly. I'm finding using this site a bit depressing lately because I make an effort to state my question clearly and format it well but get downvoted anyway simply because my question is obscure or I made a mistake. Thank you so much, really. It means a lot that some members of the community are still helpful. – Eilidh Jun 04 '14 at 12:40
0

Using JavaScript to generate PHP does NOT work. In the first case it's executed as php first, so your testFunction() is run, this resulting javascript is sent to the browser and executed.

Marek
  • 7,337
  • 1
  • 22
  • 33
0

You don't seem to understand the Server-Client model. Javascript is executed on the client, while PHP is executed on the server. The client cannot execute PHP and also cannot send PHP code to the server to be executed. It is just not possible.

bmurauer
  • 989
  • 8
  • 24
  • D'oh! I do understand the Server-Client model, it just somehow didn't register until you guys pointed it out! Thank you! – Eilidh Jun 04 '14 at 12:41