how to write javascript code inside php

前端 未结 4 1631
轻奢々
轻奢々 2020-11-28 08:23

i have a got a form, on clicking the submit button:

  1. I want to do some task in the same file (db task) AND
  2. I want the form data to be sent to test.php
4条回答
  •  -上瘾入骨i
    2020-11-28 08:42

    Lately I've come across yet another way of putting JS code inside PHP code. It involves Heredoc PHP syntax. I hope it'll be helpful for someone.

    
    

    After closing the heredoc construction the $script variable contains your JS code that can be used like this:

    
    

    The profit of using this way is that modern IDEs recognize JS code inside Heredoc and highlight it correctly unlike using strings. And you're still able to use PHP variables inside of JS code.

提交回复
热议问题