0

我已经扫描了很多天的互联网,并尝试了几乎所有发布的内容来解决问题。我想要做的是(像许多其他帖子一样),通过远程验证发送远程 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);

}
4

3 回答 3

0

行前:

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

添加 :

header("HTTP/1.1 200 OK");
于 2014-10-14T08:46:32.933 回答
0

替换php中的行

echo json_encode($result);

经过

echo json_encode(array($result));

并添加datatypejsonjs

否则你可以简单地尝试

echo 1 or 0; return;
于 2013-07-24T04:55:09.087 回答
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;
}

}

于 2016-02-08T19:21:41.673 回答