我的电子邮件检查程序的远程消息,如果数据库中可用或不可用,则无法正常运行。如果输入的电子邮件用户不可用则表示为false,则远程消息不会出现问题。
脚本:
<script>
$(function() {
$("form").validate({
rules: {
name: {
required: true,
minlength: 5
},
age: {
required: true,
minlength: 1
},
address: {
minlength: 5,
maxlength: 15,
required: true
},
gender: {
required: true
},
email: {
required: true,
email: true,
remote: {
url: "check-email.php",
type: "post"
}
}
},
messages: {
email: {
required: "Please enter your email.",
email: "Please enter a valid email.",
remote: "Email is already taken."
}
}
});
});
</script>
签email.php
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "test";
$dbc = new PDO("mysql:host=" . $host . ";dbname=" . $db, $user, $pass);
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
header('Content-type: application/json');
$requestedEmail = @$_REQUEST['email'];
$registeredEmail = "SELECT * FROM students WHERE email = :email ";
$query = $dbc->prepare($registeredEmail);
$query->bindParam(':email', $requestedEmail);
$query->execute();
$num_rows = $query->rowCount();
echo json_encode( ($num_rows === 0) ? 'true' : 'false' );
?>