1
在AJAX新手这里有这个代码,请帮助,我的代码不断去sec_reg.php页面,即使密码不匹配,甚至当表单是有效的,我希望用户即使他提交表格,也要留在当前页面上。这里是我的代码php到AJAX表单提交
这里是我的形式
<h4>ADD ANOTHER ADMIN</h4>
<form action="sec_reg.php" method="post" name="registration_form">
<br>
<p>
<strong>Email</strong>
<br>
<br>
<input class="acc_input" type="text" id="email" name="email"placeholder="Email">
</p>
<br>
<p>
<strong> Password</strong>
<br>
<br>
<input class="acc_input" type="password" name="password" id="password" placeholder="Password">
<br /><br />
<strong> Confirm Password</strong>
<br>
<br>
<input class="acc_input" type="password" name="cpassword" id="cpassword" placeholder="Confirm Password">
<input type="hidden" name="p" id="p" value="">
<br>
</p>
<button type="submit" class="btnsubmit" onclick="formhash(this.form,
this.form.password, this.form.p);" ><strong>Register</strong></button>
</form>
这里是forhash脚本(密码需要发送的安全性之前,必须哈希)
<script src="sha512.js"></script>
<script>
function formhash (form, password)
{
var pass1 = document.getElementById("password").value;
var pass2 = document.getElementById("cpassword").value;
var ok = true;
if (password != cpassword) {
//alert("Passwords Do not match");
document.getElementById("password").style.borderColor = "#E34234";
document.getElementById("cpassword").style.borderColor = "#E34234";
ok = false;
}
else {
var p = document.createElement("input");
form.appendChild(p);
p.name="p";
p.type="hidden";
p.value=hex_sha512(password.value);
password.value="";
form.submit();
}
}
</script>
这里是我的sec_reg.php
<?php
// Include database connection and functions here.
include '../Connections/mabini150_Conn.php';
if (isset($_POST['p']))
{
include 'login_Function.php';
// The hashed password from the form
$password = $_POST['p'];
// Create a random salt
$random_salt = hash('sha512', uniqid(mt_rand(1, mt_getrandmax()), true));
// Create salted password (Careful with the chilli)
$password = hash('sha512', $password.$random_salt);
$username='nousername';
$email = $_POST['email'];
if ($insert_stmt = $mysqli->prepare("INSERT INTO members (username, email, password, salt) VALUES (?, ?, ?, ?)"))
{
$insert_stmt->bind_param('ssss', $username, $email, $password, $random_salt);
// Execute the prepared query.
$insert_stmt->execute();
不错!谢谢你,主席 – user3196424
没问题。不要忘记接受答案;) –