php 如何将php变量的值传递给jquery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16946829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:56:55  来源:igfitidea点击:

How to pass php variable's value to jquery

phpjquery

提问by user61629

I have a php variable:

我有一个 php 变量:

$name_of_current_page

which I have available in my view, and I want to make the value available to jquery. Is the best way to do it like the following?

在我看来,我可以使用它,并且我想让 jquery 可以使用该值。最好的方法是像下面这样吗?

$(document).ready(function () {
            var current page = "<?php echo $name_of_current_page; ?>" ;

});

回答by tftd

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 echothe variable:

    var current page = "<?php echo $your_var; ?>";
    
  2. Twigengine:

    var current page = "{{ your_var }}";
    
  3. Smartyand RainTPLengines:

    var current page = "{$your_var}";
    
  1. 如果您使用的是普通 PHP,那么您唯一的选择就是echo变量:

    var current page = "<?php echo $your_var; ?>";
    
  2. 树枝引擎:

    var current page = "{{ your_var }}";
    
  3. SmartyRainTPL引擎:

    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.

如您所见,还有其他方法。他们都工作正常。这实际上取决于您希望如何编写和组织代码。我个人使用 Twig 并发现它非常简单、快速和直接。

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.

此外,正如其他人指出的那样,您可以对服务器进行 AJAX 调用并获取这样的变量。我发现这种方法耗时、低效且不安全。如果您选择此方法,您将向脚本发布请求。每个人都可以向该脚本发出 post/get 请求,从而为一些机器人和 DoS/DDoS 攻击打开大门。

回答by Dhamesh Makwana

var current page= "" ;

var当前页面= "" ;

I don't think you can have spaces in a variable. (i could be wrong).

我认为变量中不能有空格。(我可能是错的)。

Anyway to simplify your code, I've just done re-done it slightly.

无论如何,为了简化您的代码,我只是稍微重新做了一下。

$name_of_current_page = "HomePage";

And for the Javascript;

而对于 Javascript;

var currentPage = "<?= $name_of_current_page; ?>";

That should be it.

应该是这样。

回答by Carlos

First at all, that you ask, is a normal way on fill client side code, but this one will be load at the page load, if you want to run it on live once the page is loaded, you must use ajax, cause is the way on how you will comunicate with the server side scripts, is no possible jquery or javascript load the php vars on live once the page have been loaded

首先,你问的是填充客户端代码的正常方式,但是这个将在页面加载时加载,如果你想在页面加载后实时运行它,你必须使用ajax,原因是关于如何与服务器端脚本进行通信的方式,一旦页面被加载,jquery 或 javascript 就不可能实时加载 php vars

回答by Zach Leighton

document.titleshould give you what you need.. things like this either query the DOM or Ajax imho.

document.title应该给你你需要的东西..这样的事情要么查询DOM要么Ajax imho。

I find it best to separate layers, and not mix presentation and controller with peppered html/php code.

我发现最好将层分开,而不是将演示文稿和控制器与 html/php 代码混合在一起。