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 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据