使用 jQuery $.post 调用和回调方法设置 PHP $_SESSION['username']

发布于 2024-11-29 08:05:37 字数 1431 浏览 0 评论 0原文

我已经研究并玩了相当多的时间,但我被难住了。本质上,我想设置我的网站,以便它可以检测用户是否“登录”,从而更改其外观:删除“登录”链接并将其替换为“注销”链接,等等。

出于测试目的,我开始使用以下内容开始我的 index.html 页面:

<?php 
session_start(); 
$_SESSION["username"]="javaman";
?>

接下来,我从 jquery document.ready 中调用我的 setup 函数:

$(document).ready(function() {
    setup_page();
};

setup 函数如下所示:

function setup_page()
{
    var username = get_user();

    //check for error
    var index = username.indexOf("error");

    //if not an error
    if(username.length > 0 && index == -1)
    {
        //do the jquery calls to hide/show links
    }
}

get_user 函数如下所示:

function get_user()
{
    var result;

    $.post("./session.php", {action : "get", key : "username", value : "val"}, function(data){
            result = data;

        });

    return result;
}

session.php 是一个简单的应用程序,它需要在 3 个帖子值中,希望能输出正确的结果,我遇到的问题是 js 结果变量通常是未定义的,尤其是当我通过 IE 开发工具栏进行调试时。不过FF看起来还可以。我是否以正确的方式使用回调?我尝试在任何地方放置alert()函数来找出代码出错的地方,但这并没有帮助,因为警报经常说结果是未定义的。同时,看起来 get_user 调用了 post 函数,但堆栈立即返回,并且永远不会到达 return 语句,直到 get_user 返回一个值 .. undefined 。我相信我误解了这里的代码流程。我习惯了 C,其中逻辑上一个函数跟随另一个函数。在这种情况下,我将回调解释为本质上类似于:

int i = callback_function(post("some data"));

因此,在我看来,帖子完成了它的操作,然后调用另一个函数或至少执行另一个操作,然后完成,然后 get_user 可以返回它的值。

或者是操作顺序:post、get_user、callback?

...在西雅图感到困惑

I've researched and played around a fair bit, but I am stumped. Essentially I want to setup my site so that it can detect if a user is 'logged in' and thereby change the way it looks: removing the "Sign In" link and replacing it with a "Sign Out" link, and so forth.

For testing purposes I started my index.html page with:

<?php 
session_start(); 
$_SESSION["username"]="javaman";
?>

Next, I call my setup function from within the jquery document.ready:

$(document).ready(function() {
    setup_page();
};

The setup function looks like:

function setup_page()
{
    var username = get_user();

    //check for error
    var index = username.indexOf("error");

    //if not an error
    if(username.length > 0 && index == -1)
    {
        //do the jquery calls to hide/show links
    }
}

And that get_user function looks like:

function get_user()
{
    var result;

    $.post("./session.php", {action : "get", key : "username", value : "val"}, function(data){
            result = data;

        });

    return result;
}

The session.php is a simple app that takes in 3 post values and hopefully spits out the proper result, the problem I am running into is that the js result variable is often undefined, especially so when I debug via the IE dev toolbar. FF seems ok though. Am I using the callback in the correct way? I've tried putting alert() functions everywhere to figure out where the code is screwing up, but that doesn't help either as often the alert's say the result is undefined. Meanwhile, it seems like the get_user calls the post function but the stack immediately returns and never gets to the return statement until AFTER the get_user has returned a value of.. undefined. I believe I am misunderstanding the code flow here. I am used to C where logically one function follows another. In that vein I am interpreting the callback to essentially be like:

int i = callback_function(post("some data"));

So in my mind the post completes it's action and then calls another function or at least performs another action and then that completes and then the get_user can return it's value.

Or is the order of operation: post, get_user, callback?

...confused in Seattle

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风向决定发型 2024-12-06 08:05:37

Internet Explorer 本身不支持数组上的 indexOf。使用 jQuery 的 $.inArray() 代替:

var index = $.inArray("error", username);

Internet Explorer does not natively support indexOf on arrays. Use jQuery's $.inArray() instead:

var index = $.inArray("error", username);
秋日私语 2024-12-06 08:05:37

请记住,AJAX 代表异步Javascript 和 XML。因此,一旦响应到来,回调就会触发,但其余的执行仍会继续。如果您想锁定执行直到 AJAX 请求完成,请

$.ajaxSetup({async:false});

在 AJAX 调用之前使用。

Keep in mind that AJAX stands for Asynchronous Javascript and XML. So the callback fires as soon as a response comes, but the rest of execution goes on. If you want to lock the execution until AJAX-request will be completed, use

$.ajaxSetup({async:false});

before AJAX call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文