如果我只输入电子邮件(我已经在数据库中),则会报告错误以及我留空的其他字段错误。它要求我填写它们并且我选择的电子邮件已经被其他用户使用 但如果我完全填写表单,已经使用过的电子邮件,它不会报告显示该电子邮件已经在使用中。任何其他输入字段(如果处理不正确),包括电子邮件字段报告其错误,但如果表单完全填写则不报告可用性。
以下是代码:
<?php
if(isset($_POST['submit']))
{
if (empty($_POST["firstName"])) {
$Err[] = "* First Name is required";
} else {
$name = test_input($_POST["firstName"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$Err[] = "Only letters are allowed in First Name";
}
}
if (empty($_POST["email"])) {
$Err[] = "* Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address syntax is valid
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
$Err[] = "Invalid email format";
} else {
$emailSQL = "SELECT email FROM userdetails WHERE email = ?";
$SQ = $conn->prepare($emailSQL) or die("ERROR: " . implode(":", $conn->errorInfo()));
$SQ->bindParam(1, $email);
$SQ->execute();
$result = $SQ->fetch();
if($result > 0) {
$Err[] = "Sorry, email is already in use by another user";
}
}
}
if (empty($_POST["surname"])) {
$Err[] = "* Surname is required";
} else {
$surname = test_input($_POST["surname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$Err[] = "Only letters are allowed in Surname";
}
}
if (empty($_POST["password"])) {
$Err[] = "* Password is required";
} else {
$password = test_input($_POST["password"]);
}
if (empty($_POST["userName"])) {
$Err[] = "* Username is required";
} else {
$username = test_input($_POST["userName"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$Err[] = "Only letters are allowed in Username";
}
}
}
?>
答案 0 :(得分:-1)
我已经完成了您的查询,为什么不在丢失时检查可用性自动关注电子邮件,如果您正在刷新Html页面,那么最终用户的努力将更多地再次填写该表单。
最好使用Ajax,很容易检查可用性。
<form>Full name:
<input type="text" name="name" /><span class="error" id="nameError"></span>;
Email Address:
<input id="emailCheck" type="text" name="email" /><span class="error" id="emailError">
</form>
现在使用PHP代码:checkavailability.php
//add in your connection to the database here, mine is just a require_once statement
//get the passed parameter
$email = mysql_real_escape_string(strtolower($_POST["email"]));
//send a request to the database
$sql = "SELECT email FROM your_dbtable_name WHERE LOWER(email) = '" . $email . "'";
$result = mysql_query($sql, $conn) or die("Could not get email: " . mysql_error());
if(mysql_num_rows($result) > 0) {
//email is already taken
echo 0;
}
else {
//email is available
echo 1;
}
现在,您可以将AJax脚本保存在同一个文件或外部JS中。
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
$(document).ready(function() {
//turns off autocomplete for browsers on email field
$('#emailCheck').attr('autocomplete', 'off');
//listens for typing on the desired field
$("#emailCheck").keyup(function() {
//gets the value of the field
var email = $("#emailCheck").val();
//displays a loader while it is checking the database
$("#emailError").html("<img alt="" src="../images/loader.gif" />");
//here is where you send the desired data to the PHP file using ajax
$.post("../php/checkavailability.php", {email:email},
function(result) {
if(result == 1) {
//the email is available
$("#emailError").html("Available");
}
else {
//the email is not available
$("#emailError").html("Email not available");
}
});
}
});
});
您也可以为用户名实现相同的功能, (在具有相同功能的现代所有HTML表单中,随意询问。
由于