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