jQuery的阿贾克斯PHP - 如何确定PHP code成功执行?阿贾克斯、jQuery、code、PHP

2023-09-10 19:00:28 作者:逗

因此​​,这是我的想法,我有一个登录对话框的jQuery在我的主页,使用网页(登录表单对话框后面)会阻止任何人。我用ajax提交进入到一个PHP页面,然后检查是否用户名/密码组合存在于数据库中的信息。如果是的话,我还真然后关闭对话框(该网站允许访问)。

我如何使用Ajax来检查我的PHP脚本(从执行页)返回true或false? 我是假设的AJAX功能的成功/错误选项被选中此,但低于code似乎没有工作。

  $('登录')。对话框({
    closeOnEscape:假的,
    标题:假的,
    模式:真正的,
    宽度:'汽车',
    展会:{效应:变脸,时间:1500},
    标题:登录,
    按钮:{
        登录:函数(){
            $阿贾克斯({
                异步:假的,
                键入:POST,
                网址:SQL / login.php中',
                成功:函数(){
                    警报(成功!);
                },
                错误:函数(){
                    警报(失败!);
                },
                数据类型:HTML
            });
        }
    }
});
$(登录)对话框()父母(UI-对话。)找到(UI-对话框标题栏)。remove()方法。;
 

解决方案

我所做的就是把回声在我的PHP与variabel被放入真或假的最后,我做了成功的在这个函数选项​​是这样的:

 成功:函数(响应){
                    如果(响应){
                            警报(成功!);
                            }其他{
                            警报(失败!);
                                 }

                    }
 

这工作对我来说,你可以在PHP把这个

 如果($ MYVAR){
$ anothervar = 1;
}其他{
$ anothervar = 0;
}
回声$ anothervar;
 
PHP 使用MAMP快速搭建PHP环境

这样你就可以做到这一点:

 成功:函数(响应){
                    如果(响应== 1){
                            警报(成功!);
                            }其他{
                            警报(失败!);
                                 }

                    }
 

这是我做的方式,它工作正常 希望它是有用的。

So this is my idea, I have a login jquery dialog on my homepage that blocks anyone from using the webpage (behind the login form dialog). I use ajax to submit the information entered to a php page which then checks if the username/password combo exists in the database. If it does, I return true and then close the dialog box (allowing access to the site).

How do I use ajax to check if my php script (from the executed page) returns true or false? I was assuming the success/error options of the ajax function were checking this, but the below code doesnt seem to work.

    $('.login').dialog({
    closeOnEscape: false,
    title: false,
    modal: true,
    width: 'auto',
    show: {effect: "fade", duration: 1500},
    title: 'Login',
    buttons: {
        "Login": function() { 
            $.ajax({
                async: false,
                type: 'POST',
                url: 'sql/login.php',
                success: function(){
                    alert( "Success!");
                },
                error: function(){
                    alert( "Failed!" );
                },
                dataType: "html"
            });
        }
    }
});
$(".login").dialog().parents(".ui-dialog").find(".ui-dialog-titlebar").remove(); 

解决方案

What I did was to put an echo at the end of my php with the variabel that is put in true or false, the I did this function inside the success option it is like this:

success: function(response){
                    if(response){
                            alert( "Success!");
                            }else{
                            alert( "Failed!" );
                                 }

                    }

This worked for me, and you can put this in the php

if($myvar){
$anothervar=1;
}else{
$anothervar=0;
}
echo $anothervar;

so you can do this:

success: function(response){
                    if(response==1){
                            alert( "Success!");
                            }else{
                            alert( "Failed!" );
                                 }

                    }

this is the way that I do it and it works fine Hope it is useful