如何合并: alidateForm() - 我用它来验证表单
与
valimail(电子邮件,通行证) - 我用它来检查电子邮件是否已经在数据库中,是否不插入数据
valimail 应该以某种方式返回 true 如果电子邮件地址已经在数据库中并返回 false ,如果没有并将数据插入数据库
HTML:
<form method="POST" action="sin" name="register_form" id="regform" onsubmit="return validateForm();">
<input name="email" id="email" placeholder="E-Mail" autocomplete="off" class="" value='' type="text"><br>
<input name="pass" type="password" id="pass" placeholder="Password" autocomplete="off" value='' class=""><br>
<input name="pass1" type="password" id="pass1" placeholder="Repeat Password" autocomplete="off" value='' class=""><br>
JS:
function validateForm() {
var email = document.forms["register_form"]["email"].value;
var pass = document.forms["register_form"]["pass"].value;
var pass1 = document.forms["register_form"]["pass1"].value;
var atpos = email.indexOf("@");
var dotpos = email.lastIndexOf(".");
if (atpos< 1 || dotpos<atpos+2 || dotpos+2>=email.length) {
alert("This is not email");
return false;
}else{
if(pass.length<5||pass1.length<5 ){
alert("Password too short");
return false;
}else{
if (pass!=pass1){
alert("Passwords are not the same");
return false;
}else{
if (**here function valimail**){return false;}
}
}
}
}
function valimail(email,pass) {
$.post('checkemail.php', {'email' : email,'pass' : pass}, function(data) {
if(data=='true'){
alert("This email is already in use");
}
});
}
PHP:
$con=mysqli_connect("xxx.xxx.xxx.xxx","login","pass","db");
$email = mysqli_real_escape_string($con, $_POST['email']);
$haslo = mysqli_real_escape_string($con, $_POST['pass']);
$query = mysqli_query($con, "SELECT * FROM users WHERE email='".$email."'");
if(mysqli_num_rows($query) > 0){
echo 'true';
}else{
$sql="INSERT INTO users (email, password) VALUES ('$email', '$pass')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
}
mysqli_close($con);