dongyiba8082 2011-12-16 09:13
浏览 72
已采纳

jQuery,AJAX - 如何判断脚本是否返回false

I'm using jQuery and AJAX to validate my form when someone creates a new user on my website. I'm programming in OOP PHP, together with the jQuery and AJAX. I'm using this code:

$.ajax({  
        type: "POST",  
        url: "includes/classes/handler.php?do=addLogin",
        data: dataString,  
        success: function() {
            $('.sideBarNewUserWrap').fadeOut();
        }
    });  
    return false;

But how do I return an error message, if the e-mail already exists?

Hope it's info enough, else I'll just add some more. Thanks in forward :)

* UPDATE *

This is my PHP checking if email exists:

$email_count = mysql_num_rows($check_email);
    if($email_count){
        return false;
    }

* UPDATE *

      success: function(data){

            if(data.error){
                $('.sideBarNewUserWrap').fadeOut();
            } else {
                $('.sideBarNewUserError-email').fadeIn();
            }

Now this looks pretty much as a failure because. if(data.error) then it's okay?

Shouldn't it be something like:

if(date.error){
  //Error message
}

And not the other way around?

Well, If I try to enter an email which already exists, it tells me as it should, but why does this work? In my eyes I'm doing something wrong here?

  • 写回答

4条回答 默认 最新

  • doonbfez815298 2011-12-16 09:19
    关注

    You can get the response in the function:

    $.ajax({  
            type: "POST",  
            url: "includes/classes/handler.php?do=addLogin",
            data: dataString,  
            success: function(response) {
                if (response == "ok")
                {
                     $('.sideBarNewUserWrap').fadeOut();
                }
    
                else
                {
                    // error happend
                }
            }
        });  
        return false;
    

    You can return string, int in PHP or even XML, JSON, whatever you want to validate on client side

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流