I want my code to check if the username/email are available or already in the database without the page being refreshed. If it’s available the data will get added to the database and I will get redirected to a different page.
I already tried two ways one with jQuery which works but I can’t redirect myself to a different page, and the second one with Ajax which doesn’t seem to work at all, I’ll add the code I used on the 2 ways at the end, please feel free to point out anything that can be improved in term of security.
HTML code:
<form name="myForm" id="myForm" action="includes/user-signup.php"method="post">
<input id="signup-username" type="text" required pattern="[a-zA-Z].{2,}" name="username" placeholder="Username">
<input id="signup-pwd" type="password" name="pwd" required pattern="(?=.*\d)(?=.*[a-z]).{9,}" title="Password must contain at least 9 characters, including numbers." placeholder="Password">
<input id="signup-email" type="text" name="email" required pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" placeholder="Email">
<input id="signup-submit" type="submit" name="submit" value="SING UP" class="btn-login">
</form>
PHP code:
if (isset($_POST['submit'])){
include_once 'dbh.inc.php';
$username = mysqli_real_escape_string($conn, $_POST['username']);
$pwd = mysqli_real_escape_string($conn, $_POST['pwd']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
if(empty($username) || empty($pwd) || empty($email)){
echo "Fill in all Fields!";
}else {
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
echo "Email is invalid!";
}else{
$stmt = $conn->prepare("SELECT * FROM users WHERE username=?");
$stmt->bind_param("s", $uid);
$uid = $username;
$stmt->execute();
$result = $stmt->get_result();
$usernamecheck = mysqli_num_rows($result);
$rowNum = $result->num_rows;
if($rowNum > 0){
echo "Username is taken!";
}else{
$stmt = $conn->prepare("SELECT * FROM users WHERE email=?");
$stmt->bind_param("s", $uemail);
$uemail = $email;
$stmt->execute();
$result = $stmt->get_result();
$usernamecheck = mysqli_num_rows($result);
$rowNum = $result->num_rows;
if($rowNum > 0){
echo "Email is taken";
}else{
$hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
$stmt = $conn->prepare("INSERT INTO users (username, pwd, email) VALUES (?, ?, ?)");
$stmt->bind_param("sss",$uid, $password, $theemail);
$uid = $username;
$password = $hashedPwd;
$theemail= $email;
$stmt->execute();
$result = $stmt->get_result();
header("location: ../user-login.php");
}
}
}
}
}else{
header("location: ../user-signup.php");
exit();
}
Ajax I used which didn’t work, nothing gets submited to database:
$(document).ready(function() {
$("#myForm").submit(function(event){
event.preventDefault();
var username = $("#signup-username").val();
var pwd = $("#signup-pwd").val();
var email = $("#signup-email").val();
$(".signup-submit").text('Processing...');
$.ajax({
type: "POST",
url: "includes/user-signup.php",
data: "username="+ username + "&pwd="+ pwd + "&email="+ email,
success: function(res) {
if (res == "joined") {
console.log(res);
$(".signup-submit").text('Joined');
$("input").val('');
window.location.replace("user-login.php");
} else if(res == "Email Match") {
alert(data);
$(".signup-email").val('');
} else if (res == "failed to join") {
console.log(res);
$(".signup-submit").text('Sign Up');
} else {
// Stay Null
}
}
});
});
});
jQuery I tried, which submit data correctly to the database but instead of refreshing the page at the end it loads the new page next to the old one in .form-message
and I don’t know how to work around it:
$(document).ready(function() {
$("#myForm").submit(function(event){
event.preventDefault();
var username = $("#signup-username").val();
var pwd = $("#signup-pwd").val();
var pwd = $("#signup-email").val();
$(".form-message").load("includes/user-signup.inc.php",{
username: username,
pwd: pwd,
email:email
});
});
});
I added this to my PHP code for the jQuery to work:
$("#signup-username, #signup-pwd, #signup-email").removeClass("input-error");
var errorEmpty = "<?php echo $errorEmpty; ?>";
var errorValid = "<?php echo $errorValid; ?>";
if (errorEmpty == true $$ errorValid == true){
$("#signup-username, #signup-pwd, #signup-email").addClass("input-error");
if (errorEmpty == false && errorValid == false){
$("#signup-username, #signup-pwd, #signup-email").val("");
}