How to pass php variable's value to jquery

前端 未结 4 738
被撕碎了的回忆
被撕碎了的回忆 2020-12-24 13:19

I have a php variable:

$name_of_current_page

which I have available in my view, and I want to make the value available to jquery. Is the be

4条回答
  •  甜味超标
    2020-12-24 14:10

    It really depends if you are using some sort of a template engine.

    1. If you're using plain PHP, the only option for you is to echo the variable:

      var current page = "";
      
    2. Twig engine:

      var current page = "{{ your_var }}";
      
    3. Smarty and RainTPL engines:

      var current page = "{$your_var}";
      

    As you can see, there are other ways. All of them work fine. It really depends on how you'd like to write and organize your code. I personally use Twig and find it really easy,fast and straightforward.

    Also, as others have pointed out, you can do AJAX calls to the server and fetch the variables like that. I find that method time-consuming, inefficient and insecure. If you choose this method, you will be posting requests to a script. Everybody will be able to do post/get requests to that script which opens your doors to some bots and DoS/DDoS attacks.

提交回复
热议问题