我正在尝试设置一个注册页,它也对散列的数据进行散列,但是服务器上没有处理任何内容,而且我没有收到控制台错误,有人能告诉我我哪里出错了吗?
html代码
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<script
src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js">
</script>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="createScript.js"></script>
</head>
<body>
<div style="margin-left: 100px;">
<form method="POST" action="index.html">
<label>Enter your first name</label><br><br>
<input type="text" id="firstName" name="firstName"><br><br>
<label>Enter your last name</label><br><br>
<input type="text" id="lastName" name="lastName"><br><br>
<label>Enter a username (this will only be used to login)</label><br><br>
<input type="text" id="userName" name="userName"><br><br>
<label>Re-enter that username</label><br><br>
<input type="text" id="reuserName" name="reuserName"><br><br>
<label>Enter your email address</label><br><br>
<input type="email" id="emailAddress" name="emailAddress"><br><br>
<label>Re-enter your email address</label><br><br>
<input type="email" id="reemailAddress" name="reemailAddress"><br><br>
<label>Enter a password</label><br><br>
<input type="password" id="password" name="password"><br><br>
<label>Enter the password again</label><br><br>
<input type="password" id="rePassword" name="rePassword"><br><br>
<input type="submit" id="register" name="register">
</form>
</div>
</body>
</html>
connect.php代码
<?php
if (!defined('HOST')) define("HOST", "localhost");
if (!defined('USER')) define("USER", "root");
if (!defined('PASSWORD')) define("PASSWORD", "");
if (!defined('DB')) define("DB", "socialmedia");
$connect = new mysqli(HOST, USER, PASSWORD, DB);
if ($connect->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
?> <!-- PHP -->
create.php代码
<?php
require 'connect.php';
if ($_SERVER ['REQUEST_METHOD'] == 'POST') {
//Check for input
if((isset($_POST['firstName'])) && (!empty($_POST['firstName'])) &&
(isset($_POST['lastName'])) && (!empty($_POST['lastName'])) &&
(isset($_POST['userName'])) && (!empty($_POST['userName'])) &&
(isset($_POST['reuserName'])) && (!empty($_POST['reuserName'])) &&
(isset($_POST['emailAddress'])) && (!empty($_POST['emailAddress'])) &&
(isset($_POST['reemailAddress'])) && (!empty($_POST['reemailAddress'])) &&
(isset($_POST['password'])) && (!empty($_POST['password'])) &&
(isset($_POST['rePassword'])) && (!empty($_POST['rePassword']))) {
//Check first name
if(isset($_POST['firstName'])==0 || $_POST['firstName']==" ") {
//Error
ob_start();
echo "Please enter a valid first name";
exit;
ob_end_flush();
//Free Resources
unset($_POST['firstName']);
}//End of first name failure
else {
//Continue
$firstName = mysqli_real_escape_string($connect, $_POST['firstName']);
}//End of first name success
//Check last name
if(isset($_POST['lastName'])==0 || $_POST['lastName']==" ") {
//Error
ob_start();
echo "Please enter a valid last name";
exit;
ob_end_flush();
//Free Resources
unset($_POST['lastName']);
}//End of last name failure
else {
//Continue
$lastName = mysqli_real_escape_string($connect, $_POST['lastName']);
$fullName = mysqli_real_escape_string($connect, $firstName . " " . $lastName);
}//End of last name success
//Check username
if(isset($_POST['userName'])==0 || $_POST['userName']==" ") {
//Error
ob_start();
echo "Please enter a valid username";
exit;
ob_end_flush();
//Free Resources
unset($_POST['userName']);
}//End of username failure
else {
//Continue
$userName = mysqli_real_escape_string($connect, $_POST['userName']);
}//End of usernamesuccess
//Check reusername
if(isset($_POST['reuserName'])==0 || $_POST['reuserName']==" ") {
//Error
ob_start();
echo "Please enter a valid username";
exit;
ob_end_flush();
//Free Resources
unset($_POST['reuserName']);
}//End of reusername failure
elseif ($_POST['userName'] === $_POST['reuserName']) {
//Error
ob_start();
echo "Please enter matching usernames";
exit;
ob_end_flush();
//Free Resources
unset($_POST['reuserName']);
}
else
{
//Continue
$reuserName = mysqli_real_escape_string($connect, $_POST['reuserName']);
$hashedUserName = mysqli_real_escape_string($connect, hash('sha512', $userName));
}//End of reusername success
//Check email
if(!filter_var($_POST['emailAddress'], FILTER_VALIDATE_EMAIL)) {
//Error
ob_start();
echo "Please enter a valid email address";
exit;
ob_end_flush();
//Free Resources
unset($_POST['emailAddress']);
}//End of email failure
else {
//Continue
$emailAddress = mysqli_real_escape_string($connect, $_POST['emailAddress']);
}//End of email success
//Check reemail
if(!filter_var($_POST['reemailAddress'], FILTER_VALIDATE_EMAIL)) {
//Error
ob_start();
echo "Please enter a valid email address";
exit;
ob_end_flush();
//Free Resources
unset($_POST['reemailAddress']);
}//End of reemail failure
elseif ($_POST['emailAddress'] === $_POST['reemailAddress']) {
//Error
ob_start();
echo "Please enter matching email addresses";
exit;
ob_end_flush();
//Free Resources
unset($_POST['reemailAddress']);
}
else {
//Continue
$reemailAddress = mysqli_real_escape_string($connect, $_POST['reemailAddress']);
}//End of reemail success
//Check password
if(isset($_POST['password'])==0 || $_POST['password']==" ")
{
//Error
ob_start();
echo "Please enter a valid password";
exit;
ob_end_flush();
//Free Resources
unset($_POST['password']);
}//End of password failure
else
{
//Continue
$password = mysqli_real_escape_string($connect, $_POST['password']);
}//End of password success
//Check repassword
if(isset($_POST['rePassword'])==0 || $_POST['rePassword']==" ")
{
//Error
ob_start();
echo "Please enter a valid password";
exit;
ob_end_flush();
//Free Resources
unset($_POST['rePassword']);
}//End of repassword failure
elseif ($_POST['password'] === $_POST['rePassword']) {
//Error
ob_start();
echo "Please enter matching passwords";
exit;
ob_end_flush();
//Free Resources
unset($_POST['rePassword']);
}
else
{
//Continue
$rePassword = mysqli_real_escape_string($connect, $_POST['rePassword']);
$hashPassword = mysqli_real_escape_string($connect, hash('sha512', $password));
$saltedHashPassword = mysqli_real_escape_string($connect, hash('sha512', $userName . "" . $password));
}//End of repassword success
//Insert data into users database
$sql = "INSERT INTO users(idusers, name, username, hashedusername, email, password, unsaltedhashpassword, saltedhashpassword)
VALUES ('$fullName', '$userName', '$hashedUserName', '$email', '$password', '$hashedPassword', '$saltedHashPassword');";
if($connect->query($sql) === TRUE)
{
ob_start();
echo "<script type='text/javascript'>alert('New contact created.');</script>";
ob_end_flush();
}//End of INSERT success
else
{
ob_start();
echo "<script type='text/javascript'>alert('Error: ' . $sql . $connect->error;');</script>";
ob_end_flush();
}//End of INSERT Failed
}//Check Data
}//Get Post
?>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="createScript.js"></script>
</head>
</html>
javascript代码
$(document).ready(function(){
$("#register").click(function(){
firstName=$("#firstName").val();
lastName=$("#lastName").val();
userName=$("#userName").val();
reuserName=$("#reuserName").val();
emailAddress=$("#emailAddress").val();
reemailAddress=$("#reemailAddress").val();
pass=$("#password").val();
rePassword=$("#rePassword").val();
$.ajax({
type: "POST",
url: "create.php",
data:
"firstName="+firstName+
"&lastName="+lastName+
"&userName="+userName+
"&reuserName="+reuserName+
"&emailAddress="+emailAddress+
"&reemailAddress="+reemailAddress+
"&password="+pass+
"&rePassword="+rePassword,
success: function(html){
if(html=='true') {
console.log("Submitted!");
}
else if(html=='false'){
console.log("Error!");
}
else{
console.log("Error!"+html);
}
},
beforeSend:function()
{
},
error: function(e)
{
console.log("Error!");
console.log(e);
}
});
return true;
});
});
用户.sql
CREATE TABLE IF NOT EXISTS `users` (
`idusers` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`username` varchar(45) NOT NULL,
`hashedusername` varchar(128) NOT NULL,
`email` varchar(45) NOT NULL,
`password` varchar(45) NOT NULL,
`unsaltedhashpassword` varchar(128) NOT NULL,
`saltedhashpassword` varchar(128) NOT NULL,
PRIMARY KEY (`idusers`),
UNIQUE KEY `idusers_UNIQUE` (`idusers`),
UNIQUE KEY `username_UNIQUE` (`username`),
UNIQUE KEY `hashedusername_UNIQUE` (`hashedusername`),
UNIQUE KEY `email_UNIQUE` (`email`),
UNIQUE KEY `saltedhashpassword_UNIQUE` (`saltedhashpassword`)
)
提前谢谢你的帮助!
1条答案
按热度按时间atmip9wb1#
这可能有助于您:
当表单将数据提交到index.html时,请从表单中删除操作标记,这是错误的。
改变你的想法
.click()
对此:我刚刚补充说
e.preventDefault()
因为你的按钮是"submit"
所以它提交了表格。在ajax调用中,不需要重新加载或重定向到任何页面来保存数据或发送数据。ajax为您做这件事。这将保存您的数据
create.php
已经做了。您还可以添加
onsubmit="return false"
如果e.preventdefault()不是您的选择,则返回表单中的属性。或将按钮类型从submit
至button
这不会提交你的表格。ps:如果你觉得这个表单不错的话,试着序列化它,这样你就可以避免自己准备数据的混乱。如果需要,请尝试以下操作:
这将为你工作,你摆脱那些代码行
.val()
什么东西。干杯!!