我的isset函数不能正常工作
My isset function is not working correctly
我用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();
?>
您需要将name attribute
添加到您的输入类型
<input type="button" id="reg" name="reg" value="sign up">
如果你的insert查询是错误的,那么它将是
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
您的input
需要name
-属性,以便与POST
/GET
获取。
<input type="button" id="reg" name="reg" value="sign up">
From comments:如果你想使用这个按钮来提交表单,它需要是type=submit
的,像这样。
<input type="submit" id="reg" name="reg" value="sign up">
相关文章:
- Javascript:selenium Web驱动程序isDisplayed()不工作
- jQuery UI自动完成突然停止工作
- AngularJS UI路由器不能像ng路由器那样工作
- HTML5音频加载和播放获胜'我不能在iPad上工作
- JavaScript打印功能使日历停止工作
- Javascript.getHours()工作不正常
- 为什么这在IE中的工作方式与在Firefox中不同
- 视频HTML没有'无法在Internet Explorer 11上工作
- 扩展移相器按钮类不工作
- Firebase迁移-简单的Firebase.set没有'不再工作了——旧的还是新的
- 谷歌地图不是以HTML显示,而是在JS Fiddle上工作
- 正在尝试使用if和else添加类,但无法正常工作
- Jquery FadeIn FadeOut 只工作一次
- Foreach无法在Typescript中工作
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 为什么不是't窗口.恢复正常工作吗?(javascript/jquery)
- JS可以在Chrome中工作,但不能在Firefox中工作
- 最大高度转换不;不工作,工作缓慢
- Gulp-rev:不是第一次工作,而是在第一次工作之后工作
- JQuery - Ajax: encodeUriComponent不工作(EncodeUri工作)