我使用jquery和php创建了一个注册表单,它动态检查用户输入的数据。
当我点击按钮时,没有任何内容被发送到数据库,我知道查询有效,因为当我删除isset函数时,空白数据会被发送到数据库。
任何帮助都将非常感激。
html
<DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Tutorial</title>
</head>
<body>
<form>
<input type="text" id="fname" size ="25" placeholder="firstname" />
<span id="fname_feedback"></span></br></br>
<input type="text" id="lname" size ="25" placeholder="lastname" />
<span id="lname_feedback"></span></br></br>
<input type="text" id="username" size ="25" placeholder="username"/>
<span id="username_feedback"></span></br></br>
<input type="text" id="email" size ="25" placeholder="email"/>
<span id="email_feedback"></span></br></br>
<input type="text" id="confirm_email" size ="25" placeholder="confirm email"/>
<span id="confirm_email_feedback"></span></br></br>
<input type="text" id="password" size ="25" placeholder="password"/>
<span id="password_feedback"></span></br></br>
<input type="text" id="confirm_password" size ="25" placeholder="confirm password"/>
<span id="confirm_password_feedback"></span></br></br>
<input type="submit" id="reg" name="reg" value="sign up">
</form>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/reg.js"></script>
</body>
</html>
jquery的
function validate(fname, lname, username, email, confirm_email, password, confirm_password){
$.post('php/registration.php', {
fname: fname,
lname: lname,
username: username,
email: email,
confirm_email: confirm_email,
password: password,
confirm_password: confirm_password,
},
function(data){
$('#fname_feedback').text(data.fname);
$('#lname_feedback').text(data.lname);
$('#username_feedback').text(data.username);
$('#email_feedback').text(data.email);
$('#confirm_email_feedback').text(data.confirm_email);
$('#password_feedback').text(data.password);
$('#confirm_password_feedback').text(data.confirm_password);
},'json');
}
/////////////////////////////////fname jquery///////////////////////////////////
$('#fname').focusin(function(){
if($('#fname').val() ==='') {
$('#fname_feedback').text('requried field');
} else {
validate($('#fname').val(), "","","","","","");
}
}).blur(function() {
validate($('#fname').val(), "","","","","","");
}).keyup(function(){
validate($('#fname').val(), "","","","","","");
});
/////////////////////////////////lname jquery///////////////////////////////////
$('#lname').focusin(function(){
if($('#lname').val() ==='') {
$('#lname_feedback').text('requried field');
} else {
validate("", $('#lname').val(),"","","","","");
}
}).blur(function() {
validate("", $('#lname').val(),"","","","","");
}).keyup(function(){
validate("", $('#lname').val(),"","","","","");
});
/////////////////////////////////username jquery///////////////////////////////////
$('#username').focusin(function(){
if($('#username').val() ==='') {
$('#username_feedback').text('requried field');
} else {
validate("","", $('#username').val(),"","","","");
}
}).blur(function() {
$('#username_feedback').text('');
validate("","", $('#username').val(),"","","","");
}).keyup(function(){
validate("","", $('#username').val(),"","","","");
});
/////////////////////////////////email jquery///////////////////////////////////
$('#email').focusin(function(){
if($('#email').val() ==='') {
$('#email_feedback').text('requried field');
} else {
validate("","","", $('#email').val(),"","","");
}
}).blur(function() {
$('#email_feedback').text('');
validate("","","", $('#email').val(),"","","");
}).keyup(function(){
validate("","","", $('#email').val(),"","","");
});
/////////////////////////////////confirm email jquery///////////////////////////////////
$('#confirm_email').focusin(function(){
if($('#confirm_email').val() ==='') {
$('#confirm_email_feedback').text('requried field');
} else {
validate("","","",$('#email').val(), $('#confirm_email').val(),"","");
}
}).blur(function() {
$('#confirm_email_feedback').text('');
validate("","","",$('#email').val(), $('#confirm_email').val(),"","");
}).keyup(function(){
validate("","","",$('#email').val(), $('#confirm_email').val(),"","");
});
/////////////////////////////////password jquery///////////////////////////////////
$('#password').focusin(function(){
if($('#password').val() ==='') {
$('#password_feedback').text('requried field');
} else {
validate("","","","","", $('#password').val(),"");;
}
}).blur(function() {
$('#password_feedback').text('');
validate("","","","","", $('#password').val(),"");
}).keyup(function(){
validate("","","","","", $('#password').val(),"");
});
///////////////////////////////// confirm password jquery///////////////////////////////////
$('#confirm_password').focusin(function(){
if($('#confirm_password').val() ==='') {
$('#confirm_password_feedback').text('requried field');
} else {
validate("","","","","",$('#password').val(), $('#confirm_password').val());
}
}).blur(function() {
$('#confirm_password_feedback').text('');
validate("","","","","",$('#password').val(), $('#confirm_password').val());
}).keyup(function(){
validate("","","","","",$('#password').val(), $('#confirm_password').val());
});
$( "#reg" ).click(function() {
alert( "form submitted." );
});
PHP
<?php
require '../init.php';
$fname_error= '';
$lname_error='';
$username_error='';
$email_error='';
$confirm_email_error='';
$password_error='';
$confirm_password_error='';
$d = date("Y-m-d"); // Year - Month - Day
if( isset($_POST['fname']) && $_POST['fname'] !== "")
{
$fname = $_POST['fname'];
$fname = trim($fname);
if(strlen($fname) < 5 || strlen($fname) > 15) {
$fname_error = 'min characters is 5 max 15';
} else {
$fname_error= '';
}
}
if( isset($_POST['lname']) && $_POST['lname'] !== "")
{
$lname = $_POST['lname'];
$lname = trim($lname);
if(strlen($lname) < 5 || strlen($lname) > 20) {
$lname_error = 'min characters is 5 max 20';
} else {
$lname_error = '';
}
}
if(isset($_POST['username']) && $_POST['username'] !== ""){
$username = $_POST['username'];
$username = trim($username);
$u_check = mysqli_query($connect,"SELECT username FROM users WHERE username='$username'");
// Count the amount of rows where username = $username
$check = mysqli_num_rows($u_check);
if ($check == 0) {
if(strlen($username) < 5 || strlen($username) > 20) {
$username_error = 'username has to be between 5 and 20 characters';
} else {
$username_error='';
}
} else {
$username_error = 'username taken';
}
}
if(isset($_POST['email']) && $_POST['email'] !== ""){
$email = $_POST['email'];
$email = trim($email);
//Check whether Email already exists in the database
$email_check = mysqli_query($connect,"SELECT email FROM users WHERE email='$email'");
//Count the number of rows returned
$email_result = mysqli_num_rows($email_check);
if ($email_result == 0) {
$email_error ='';
} else {
$email_error = 'email taken';
}
}
if(isset($_POST['confirm_email']) && $_POST['confirm_email'] !== ""){
$confirm_email = $_POST['confirm_email'];
$confirm_email = trim($confirm_email);
if ($confirm_email == $email) {
$confirm_email_error ='';
} else {
$confirm_email_error ='nope';
}
}
if(isset($_POST['password']) && $_POST['password'] !== ""){
$password = $_POST['password'];
$password = trim($password);
if (strlen($password)>30||strlen($password)<5) {
$password_error = "Your password must be between 5 and 30 characters long!";
} else {
$password_error ='';
}
}
if(isset($_POST['confirm_password']) && $_POST['confirm_password'] !== ""){
$confirm_password = $_POST['confirm_password'];
$confirm_password= trim($confirm_password);
if ($password == $confirm_password) {
$confirm_password_error ='';
$password = md5($password);
} else {
$confirm_password_error ='nope';
}
}
$query = mysqli_query($connect,"INSERT INTO users(first_name, last_name,username,email,password) VALUES ('', '$fname', '$lname', '$username', '$email', '$password', '$d', '0','','','')");
$return_data=array('fname'=> $fname_error, 'lname'=>$lname_error, 'username'=> $username_error,'email'=>$email_error,'confirm_email'=>$confirm_email_error, 'password'=> $password_error, 'confirm_password'=>$confirm_password_error);
header('Content-Type: application/json');
echo json_encode($return_data);
exit();
?>