Jquery远程验证规则 - php电子邮件检查失败

时间:2013-07-24 04:52:03

标签: php jquery json validation jquery-plugins

我现在已经扫描了互联网很多天了,并且已经尝试过所有发布的内容来解决问题。我想做的是(像许多其他帖子一样),通过远程验证发送一个远程mysql查询..关于返回数据的正确格式(如json_encode与否)有很多争论,我已经尝试了两个建议没有用。

Jquery代码

    $('#register-form-step-1').validate({  // initialize plugin
    rules:
    {
        confirmEmail:
        {
            equalTo: "#clientEmailAddress"
        },
        clientPassword:
        {
            rangelength: [6,32],
            required: true
        },
        clientUserName:
        {
            minlength: 4,
            required: true,
            remote:
            {
                async:false,
                type:'POST',
                url:'<?php echo base_url("home/checkusername")?>',
                data: {
                clientUserName: function() {
                return $("#clientUserName").val();
                }},
                success: function(data)
                {
                    console.log(data);
                    if (String(data) === String('true'))
                    {
                        //not registered
                        console.log("Not registered");
                        return true;
                    }
                    else
                    {
                    console.log(data);
                        //already registered
                        console.log("Already registered");
                    }
                },
                error: function()
                {
                    console.log("There was an error");
                }
            }
        },
        clientEmailAddress:
        {
            async:false,
            required: true,
            email: true,
            remote:
            {
                type:'POST',
                url:'<?php echo base_url("home/checkemail")?>',
                data: {
                clientEmailAddress: function() {
                return $("#clientEmailAddress").val();
                }},
                success: function(data)
                {
                    console.log(data);
                    if (String(data) === String('true'))
                    {
                        //not registered
                        console.log("Not registered");
                        return true;
                    }
                    else
                    {
                        //already registered
                        console.log("already registered");
                    }
                },
                error: function()
                {
                    console.log("There was an error");
                }
            }       
        }
    },
    submitHandler: function ()
    {
        $.ajax({
            type: 'POST',
            url: '<?php echo base_url("home/register")?>',
            data: $('#register-form-step-1').serialize(),
            success: function ()
            {
                alert('success')
                console.log('form was submitted');
                $("#register-form-1").modal('hide');
                $("#register-form-2").modal('show');
            },
            error: function(data, textStatus, jqXHR) {
                  alert('error')
            }
        });

        return false; // ajax used, block the normal submit
    }
});

PHP代码

    public function checkemail()
{

    $email = mysql_real_escape_string($_POST['clientEmailAddress']);

    $qResult = $this->db->query('
    SELECT clientEmailAddress FROM clientdata WHERE clientEmailAddress = "'.$email.'" limit 1
    ');

    $result = true;

    if ($qResult->num_rows == 1)
    {
        $result = false;
    }

    header('Content-Type: application/json');
    echo json_encode($result);

}

3 个答案:

答案 0 :(得分:0)

替换 php

中的行
echo json_encode($result);

通过

echo json_encode(array($result));

并在datatype

中添加json js

否则你可以试试

echo 1 or 0; return;

答案 1 :(得分:0)

行前:

header('Content-Type: application/json');

添加:

header("HTTP/1.1 200 OK");

答案 2 :(得分:0)

我有同样的问题,但最后样本功能为我做了。

function checkIfEmailExists($email){
    $query="SELECT email FROM users WHERE email='".$email."'";
    $link=@mysql_query($query);
    $count=mysql_num_rows($link);

    $response='';
    if($count==1){
        $response=false;
    }else if($count==0){
        $response=true;
    }
    header('Content-Type: application/json');
    echo json_encode($response);
    return;
}

}