0

I have been scanning the interwebs for many days now, and have tried just about everything posted to resolve the issue. What i am trying to do is (like many other posts), send a remote mysql query via remote validation.. there has been much debate on the proper format of the return data (like json_encode or not) and i have tried both suggestions to no avail.

Jquery Code

    $('#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 CODE

    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

私は同じ問題を抱えていましたが、最終的にサンプル機能がそれをやってくれました。

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 に答える
0

PHPの行を置き換えます

echo json_encode($result);

echo json_encode(array($result));

のように追加datatypeしますjsonjs

それ以外の場合は、単に試すことができます

echo 1 or 0; return;
于 2013-07-24T04:55:09.087 に答える
0

行の前:

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

追加 :

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