我已经为表单验证实现了jquery验证引擎插件。问题在于输入的ajax验证。无论何时提交表单或在显示ajax成功消息之前单击提交按钮,它都会重定向到同一页面,这意味着表单提交上的php脚本不会调用。
例如:
我有一个电子邮件地址
<input class="validate[required,ajax[EmailCallPhp]]" type="text" name="email" />
当我们输入电子邮件并在之前点击提交按钮并且任何成功/错误如果ajax返回false表单未提交但如果成功,则提交表单但表单提交上的php脚本不起作用并重定向到同页..
$(document).ready(function(){
$("#RegFb").validationEngine();
});
代码调用ajax:
"EmailCallPhp": {
"url": "valEmail.php", //register email validation
// you may want to pass extra data on the ajax call
"extraData": "name=eric",
// if you provide an "alertTextOk", it will show as a green prompt when the field validates
"alertTextOk": "* OK",
"alertText": "* Email Already Exist",
"alertTextLoad": "* Validating, please wait"
}
valEmail.php代码
$email=$_REQUEST['fieldValue'];
$emailId=$_REQUEST['fieldId'];
$validateError= "This username is already taken";
$validateSuccess= "This username is available";
/* RETURN VALUE */
$arrayToJs = array();
$arrayToJs[0] = $emailId;
$stmt = $db->prepare("SELECT email FROM table WHERE email = :email");
$stmt->execute(array(':email'=>$email));
if($stmt->rowCount() == 0){ // validate??
$arrayToJs[1] = true; // RETURN TRUE
echo json_encode($arrayToJs); // RETURN ARRAY WITH success
}else{
for($x=0;$x<1000000;$x++){
if($x == 990000){
$arrayToJs[1] = false;
echo json_encode($arrayToJs); // RETURN ARRAY WITH ERROR
}
}
}
谢谢!