PHP code inside a Javascript's “[removed]()”

后端 未结 5 882
广开言路
广开言路 2020-12-20 21:50

I need to write some PHP code inside a Javascript\'s \"document.write()\". This is a dummy example, but in the future Javascript will generate automatically tha

相关标签:
5条回答
  • 2020-12-20 22:35

    The PHP gets evaluated before the JavaScript, so no need to escape the quotes.

    document.write("<?php echo "Hello World"; ?>");
    
    0 讨论(0)
  • 2020-12-20 22:46

    If you need to execute some PHP code via javascript, you would need to have the PHP housed on a server somewhere where it can be run (i.e. the browser client will not run it) and then you need to used AJAX to run that script and do whatever you need to do with its output.

    0 讨论(0)
  • 2020-12-20 22:51

    It's impossible. Php need server-side to runs, and javascript runs just on client side. What you can do is prepare one php code to write one file and call this code by javascript.

    0 讨论(0)
  • 2020-12-20 22:53

    What you ask is not possible. Javascript generates the php code client side, after the server is finished. The server never sees the PHP code.

    0 讨论(0)
  • 2020-12-20 22:54

    when javascript runs that means the response is showing to the browser not compiling the php code any more so as you are putting php code in javascript that means you want something from server than no other alternate but ajax.

    0 讨论(0)
提交回复
热议问题