我有一个注册表,我正在使用ajax
< form id="form" class="form" action="form.php" method="post" >
在form.php
单击按钮注册时,验证以表格形式发生
就像用户名存在和密码匹配然后它注册它在form.php中工作正常但是对于ajax我必须创建另一个页面check.php
,它还检查用户是否存在ajaxusername = username
然后它返回用户存在于前面用户名字段的确定,但是当我按下注册时,即使用户存在,它也会注册用户。
我使用了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";
}
}
?>