I have a registration form , I'm using ajax
< form id="form" class="form" action="form.php" method="post" >
In form.php
when I click button register the validations happens in form
like if username exists and password match then it registers it all works fine in form.php but for ajax i had to create another page check.php
which also checks user existence if ajaxusername = username
then it returns user exist in front of the username field which is ok but when i press register it register the user even if the user exists.
I have used ajax :
---------------- form . php -------------
function aj(){
$("document").ready(function (){
var username = $("#username").val();
$.ajax({
url: 'check.php',
type: 'POST',
data: {ajaxusername:username},
success: function(data) {
$('#existu').html(data);
}
});
});
};
<?php
session_start();
$con=mysqli_connect("localhost","user","","db") or
die(mysql_error());
if(isset($_POST['register'])){
$name=$_POST['username'];
$email=$_POST['email'];
$password=$_POST['password'];
$confirmpassword=$_POST['confirmpassword'];
$q=mysqli_query($con,"SELECT * FROM userreg WHERE username='$name'");
$query="INSERT INTO regform(username,email,password,confirmpassword)
VALUES('$name','$email','$password','$confirmpassword')";
if(mysqli_num_rows($q) > 0){
$userexist="User already exists!";
}
elseif ($password==$confirmpassword){
mysqli_query($con,$query);
$a=header("location:welcome.php");
}
else{
$error="Password does not match!";
}
}
?>
check.php
session_start();
$con=mysqli_connect("localhost","user","","db") or die(mysql_error());
if(isset($_POST['ajaxusername'])){
$query="SELECT * FROM userreg WHERE username='".$_POST["ajaxusername"]."'";
$result=mysqli_query($con,$query);
if(mysqli_num_rows($result)>0){
echo "exist";
}
else
{
echo "does not exist";
}
}
?>