我试图避免在MySQL数据库中添加重复的电子邮件地址。是否有任何javascript方式检查没有重新加载?就像在Gmail登录表单中一样?
答案 0 :(得分:0)
使用验证库:http://docs.jquery.com/Plugins/Validation/Methods/remote
你的javascript:
$("#yourFormId").validate({
rules: {
email: {
required: true,
email: true,
remote: {
url: "checkmail.php",
type: "post"
}
}
},
messages: {
email: {
required: "Please Enter Email!",
email: "This is not a valid email!",
remote: "Email already in use!"
}
}
});
checkmail.php:
<?php
$registeredEmails = array('test1@test.com', 'test2@test.com', 'test3@test.com');
$requestedEmail = $_POST['email'];
if( in_array($requestedEmail, $registeredEmails) ){
echo 'false';
}
else{
echo 'true';
}
?>
你可以搜索mysql发布的电子邮件地址而不是$ registeredEmails数组:)