Insert Data With Ajax
<html>
<head></head>
<body>
<h1>Registration Form</h1>
<form id="regform" action="" method="post">
<fieldset style="width:40%">
<legend>Register Here..</legend>
<span id="error"></span>
<br/>
<p>Name :<input type="text" name="name"/></p>
<p>Father Name :<input type="text" name="fname"/></p>
<p>Mobile :<input type="text" name="mobile"/></p>
<p>Email:<input type="email" name="email"/></p>
<p>Password:<input type="password" name="pass"/></p>
<p>Gender :Male<input type="radio" name="gender" value="male" />
Female<input type="radio" name="gender" value="fimale" /></p>
<input type="submit" id="btn" name="btn"/>
</fieldset>
</form>
<script type="text/javascript" src="jquery-3.6.0.min.js"></script>
<script type="text/javascript">
$('#regform').on('submit',function(e){
$('#btn').val('Please Wait..');
$('#btn').attr('daisabled',true);
$.ajax({
url:"regsubmit.php",
type:'POST',
data:$('#regform').serialize(),
success:function(response){
$('#error').html(response);
$('#regform')['0'].reset();
$('#btn').val('Submit');
$('#btn').attr('daisabled',false);
}
});
e.preventDefault();
});
</script>
</body>
</html>
regsubmit.php
<?php
sleep(3);
$name=isset($_POST['name'])?$_POST['name']:Null;
$fname=isset($_POST['fname'])?$_POST['fname']:Null;
$mobile=isset($_POST['mobile'])?$_POST['mobile']:Null;
$email=isset($_POST['email'])?$_POST['email']:Null;
$pass=isset($_POST['pass'])?$_POST['pass']:Null;
$gender=isset($_POST['gender'])?$_POST['gender']:Null;
if (is_null($name) or empty($name)) {
echo "Name is Not empty";
exit;
}
if (is_null($fname) or empty($fname)) {
echo "Father Name is Not empty";
exit;
}
if (is_null($mobile) or empty($mobile)) {
echo "mobile is Not empty";
exit;
}
if (!preg_match("/^[6-9]{1}[0-9]{9}$/",$mobile)) {
echo "mobile is Not Valid";
exit;
}
if (is_null($email) or empty($email)) {
echo "email is Not empty";
exit;
}
if (is_null($pass) or empty($pass)) {
echo "Password is Not empty";
exit;
}
if (is_null($gender) or empty($gender)) {
echo "gender Could Not be selected";
exit;
}
$con=mysqli_connect("localhost:3308","root","","ajax_db") or die("Connection Error".mysqli_connect_error($con));
$sql="Insert into user(name,father_name,mobile,email,password,gender) values('$name','$fname','$mobile','$email','$pass','$gender')";
$res=mysqli_query($con,$sql);
if ($res) {
echo "Recorcd Inserted";
}else{
echo "Recorcd Not Inserted";
}
Click Here Download Source Code ScreenShot -
1) . Html Source Code
0 Comments