Javascript 如何在 jQuery 中回显 PHP?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5895822/
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
How can I echo PHP within jQuery?
提问by bob_cobb
I'm trying to use one of my PHP functions within one of my jQuery functions. I understand that it won't execute the code, but I need to echo the function call, so the server can process it. So far I have this:
我正在尝试在我的 jQuery 函数之一中使用我的 PHP 函数之一。我知道它不会执行代码,但我需要回显函数调用,以便服务器可以处理它。到目前为止,我有这个:
.html('<h2>Please <a href=""<?php echo absolute_url("login.php"); ?>"">login</a> or <a href="signup.php">register</a> to vote for this post.</h2>(click on this box to close)')
But it's not working correctly. I heard that I need to enclose the actual php function call within Javascript with quotation marks, which I did (both single and double), but they didn't do the trick. Any ideas?
但它不能正常工作。我听说我需要用引号将 Javascript 中的实际 php 函数调用括起来,我这样做了(单引号和双引号),但他们没有做到这一点。有任何想法吗?
The whole function for anyone wondering:
任何想知道的人的整个功能:
// login or register notification
$(document).ready(function() {
$('.notice').click(function() {
$('.error-notification').remove();
var $err = $('<div>').addClass('error-notification')
.html('<h2>Please <a href=""<?php echo absolute_url("login.php"); ?>"">login</a> or <a href="signup.php">register</a> to vote for this post.</h2>(click on this box to close)')
.css('left', $(this).position().left);
$(this).after($err);
$err.fadeIn(150);
});
$('.error-notification').live('click', function() {
$(this).fadeOut(150, function() {
$(this).remove();
});
});
});
回答by Atticus
You can't -- php commands run during page loading -- your page has already loaded and rendered. You should know php is server sidecode. This means when your website is requested, your server generates the output file and fills in all the echo values into your output file. The client gets the page with all the variables echo'd to your page -- no php commands are ever given to a client, they're instructions for your server to compile the webpage back to the client.
你不能——在页面加载期间运行 php 命令——你的页面已经加载和呈现。您应该知道 php 是服务器端代码。这意味着当您的网站被请求时,您的服务器会生成输出文件并将所有回声值填充到您的输出文件中。客户端获取页面,所有变量都回显到您的页面 - 没有向客户端提供任何 php 命令,它们是您的服务器将网页编译回客户端的指令。
Why don't you just store absolute_url("login.php") within a javscript variable during your pageload
为什么不在页面加载期间将 absolute_url("login.php") 存储在 javscript 变量中
var loginUrl = "<?php echo absolute_url("login.php"); ?>";
and then
进而
.html('<h2>Please <a href="' + loginUrl + '">Login</a>')....
Edit -- oh wait duh -- you are doing that :P sorry for the tangent then, your js file is linked to your page, it's a stub pointing to the file to be included. This does not register php commands.
编辑 - 哦,等等 - 你正在这样做 :P 对不起,切线然后,你的 js 文件链接到你的页面,它是一个指向要包含的文件的存根。这不会注册 php 命令。
回答by Treffynnon
This will not actually do anything as you have noted in your question but try:
这实际上不会像您在问题中指出的那样做任何事情,但请尝试:
$('elem').html('<h2>Please <a href="<?php echo absolute_url("login.php"); ?>">login</a> or <a href="signup.php">register</a> to vote for this post.</h2>(click on this box to close)');
If this is coming from a PHP page then try:
如果这是来自 PHP 页面,请尝试:
$('elem').html('<h2>Please <a href="<?php echo absolute_url("login.php"); ?>">login</a> or <a href="signup.php">register</a> to vote for this post.</h2>(click on this box to close)');
Obviously the PHP interpreter will never see this and the PHP code will never be executed as you have already noted in your question.
显然,PHP 解释器永远不会看到这一点,并且 PHP 代码永远不会执行,正如您在问题中已经指出的那样。
回答by Headshota
you can't do this in your js file, but you can do this in a <script>
tag in HTML file.
您不能在 js 文件中执行此操作,但可以<script>
在 HTML 文件中的标记中执行此操作。
.html('<h2>Please <a href="<?php echo absolute_url(\"login.php\"); ?>">login</a>
or <a href="signup.php">register</a> to vote for this post.
</h2>(click on this box to close)')
回答by fat
Have you checked the source code of the result page? i would use only one " before and after php tags
你检查过结果页的源代码吗?我只会在 php 标签之前和之后使用一个 "
回答by Avi Kapuya
I think the proper way to use php from Javascript is by creating web services (in php) and call them from your Javascript. If you want to go really wild, you can send the php content to the server using a web service, write it as a temp file, then 'require' the file and run it... this is quite an off thing to do unless there is a very unique use case that cannot be done otherwise. PHP, as an extremely dynamic language allow this kind of usage.
我认为从 Javascript 中使用 php 的正确方法是创建 Web 服务(在 php 中)并从您的 Javascript 中调用它们。如果您想变得非常疯狂,您可以使用 Web 服务将 php 内容发送到服务器,将其编写为临时文件,然后“要求”该文件并运行它...有一个非常独特的用例,否则无法完成。PHP,作为一种极其动态的语言,允许这种用法。
回答by K6t
y u go for php??? i thing its not possible in js,,
你去 php 吗???我认为在 js 中是不可能的,
best thing is use html its very effective and easy to console..
最好的事情是使用 html,它非常有效且易于控制台。
.html('<h2>Please <a href="login.php">login</a> or <a href="signup.php">register</a> to vote for this post.</h2>(click on this box to close)')