如何在jQuery中回显PHP? [英] How can I echo PHP within jQuery?

查看:149
本文介绍了如何在jQuery中回显PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的jQuery函数之一中使用我的PHP函数之一.我知道它不会执行代码,但是我需要回显函数调用,以便服务器可以处理它.到目前为止,我有这个:

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:

    .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)')

但是它不能正常工作.我听说我需要在Javascript中用引号将实际的php函数调用括起来,我做到了(单引号和双引号),但是他们没有做到这一点.有什么想法吗?

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?

任何人想知道的全部功能:

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();
        });
    });
});

推荐答案

您不能-在页面加载期间运行php命令-您的页面已经加载并呈现.您应该知道php是服务器端代码.这意味着当请求您的网站时,服务器将生成输出文件,并将所有回显值填充到您的输出文件中.客户端获取的页面具有与您的页面回显的所有变量-从未向客户端提供任何php命令,它们是您的服务器将网页编译回客户端的说明.

You can't -- php commands run during page loading -- your page has already loaded and rendered. You should know php is server side code. 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.

为什么不在页面加载期间将absolute_url("login.php")存储在javscript变量中

Why don't you just store absolute_url("login.php") within a javscript variable during your pageload

var loginUrl = "<?php echo absolute_url("login.php"); ?>";

然后

.html('<h2>Please <a href="' + loginUrl + '">Login</a>')....

编辑-等等,您正在这样做:P切线,抱歉,您的js文件已链接到您的页面,它是指向要包含的文件的存根.这不会注册php命令.

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.

这篇关于如何在jQuery中回显PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆