我正在为使用jQuery
and的应用程序开发用户注册和登录PHP
。我在将记录插入我的成员数据库时遇到问题。当我点击提交时,我收到“未注册”警报,它只是将我带回注册页面。
我jQuery
的提交按钮句柄和HTML
:
$('#regsubmit').click(function(){
$.post("register.php",{reguser: $("#reguser").val(), fname: $("#fname").val(),lname: $("#lname").val(),regpass: $("#regpass").val(), regemail: $("#regemail").val()},function(data){
if(data == true){
alert("Registered");
}else{
alert("Not Registered");
}
});
});
<div data-role="content">
<div data-role="collapsible"><h2>Register</h2>
<form action="" method="post" id="registrationform">
<div data-role="fieldcontain">
<label for="fname">First name:</label>
<input type="text" name="fname" id="fname" value="" />
<div id="fnamecheck"></div>
</div>
<div data-role="fieldcontain">
<label for="lname">Last name:</label>
<input type="text" name="lname" id="lname" value="" />
<div id="lnamecheck"></div>
</div>
<div data-role="fieldcontain">
<label for="regemail">Email:</label>
<input type="email" name="regemail" id="regemail" value="" />
<div id="emailcheck"></div>
</div>
<div data-role="fieldcontain">
<label for="reguser">Username:</label>
<input type="text" name="reguser" id="reguser" value="" />
<div id="usernamecheck"></div>
</div>
<div data-role="fieldcontain">
<label for="regpass">Password:</label>
<input name="regpass" type="password" id="regpass" value="">
<div data-role="fieldcontain"><label for="confirmregpass">Confirm Password:</label>
<input name="confirmregpass" type="password" id="confirmregpass" value=""></div>
</div>
<input name="regsubmit" type="submit" id="regsubmit" value="Register" data-icon="check" data-theme="a"/>
</form>
</div>
还有我的 register.php:
<?php
$MYSQL_SERVER = "localhost";
$MYSQL_USER = "root";
$MYSQL_PASSWORD = "password";
$db = mysql_connect($MYSQL_SERVER, $MYSQL_USER, $MYSQL_PASSWORD ) or die(mysql_error());
mysql_select_db("hedonsof_conflict") or die(mysql_error());
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['regemail'];
$reguser = $_POST['reguser'];
$regpass = $_POST['regpass'];
//Username check
$usercheck = mysql_query("SELECT * FROM members WHERE username='".$reguser."'");
if(empty($fname)||empty($lname)||empty($email)||empty($reguser)||empty($regpass)){
if(empty($fname)){
$errors[] = "Missing first name.";
}
if(empty($lname)){
$errors[]= "Missing last name.";
}
if(empty($email)){
$errors[]= "Missing email.";
}
if(empty($reguser)){
$errors[]= "Missing user name.";
}
if(empty($regpass)){
$errors[] = "Missing password.";
}
}elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$errors = "Not a valid eail address.";
}else{
$fname = strip_tags($fname);
$fname = stripslashes($fname);
$fname = trim($fname);
$lname = strip_tags($lname);
$lname = stripslashes($lname);
$lname = trim($lname);
$email = strip_tags($email);
$email = stripslashes($email);
$email = trim($email);
$reguser = strip_tags($reguser);
$reguser = stripslashes($reguser);
$reguser = trim($reguser);
$regpass = strip_tags($regpass);
$regpass = stripslashes($regpass);
$regpass = trim($regpass);
$sql = mysql_query("INSERT INTO members (username, fname, lname, password, email) VALUES('$reguser','$fname','$lname','$regpass','$email)") or die(mysql_error());
$msg = "Thanks for Registering.";
if($msg){
echo true;
}else{
$errors[] = "Sorry error with database at this time.";
echo $errors;
}
}
?>
编辑:
<?php
$MYSQL_SERVER = "localhost";
$MYSQL_USER = "root";
$MYSQL_PASSWORD = "password";
$db = mysql_connect($MYSQL_SERVER, $MYSQL_USER, $MYSQL_PASSWORD ) or die(mysql_error());
mysql_select_db("hedonsof_conflict") or die(mysql_error());
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['regemail'];
$reguser = $_POST['reguser'];
$regpass = $_POST['regpass'];
$errors[] = "";
//Username check
$usercheck = mysql_query("SELECT * FROM members WHERE username='".$reguser."'");
if(empty($fname)){
$errors[] = "Missing first name.";
}
if(empty($lname)){
$errors[]= "Missing last name.";
}
if(empty($email)){
$errors[]= "Missing email.";
}
if(empty($reguser)){
$errors[]= "Missing user name.";
}
if(empty($regpass)){
$errors[] = "Missing password.";
}
// }elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
// $errors = "Not a valid email address.";
else{
$fname = strip_tags($fname);
$fname = stripslashes($fname);
$fname = trim($fname);
//$fname = mysqli_real_escape_string($fname);
$lname = strip_tags($lname);
$lname = stripslashes($lname);
$lname = trim($lname);
//$lname = mysqli_real_escape_string($lname);
$email = strip_tags($email);
$email = stripslashes($email);
$email = trim($email);
//$email = mysqli_real_escape_string($email);
$reguser = strip_tags($reguser);
$reguser = stripslashes($reguser);
$reguser = trim($reguser);
//$reguser = mysqli_real_escape_string($reguser);
$regpass = strip_tags($regpass);
$regpass = stripslashes($regpass);
$regpass = trim($regpass);
//$regpass = mysqli_real_escape_string($regpass);
$sql = mysql_query("INSERT INTO members (username, fname, lname, password, email) VALUES('$reguser','$fname','$lname','$regpass','$email')") or die(mysql_error());
$msg = "Thanks for Registering.";
if($msg){
echo "true";
}else{
$errors[] = "Sorry error with database at this time.";
echo "false";
}
}
?>