AJAX form auto submit wthout refresh page

  1. Html Form
<form  id="form" method="POST" action="form_submit.php">
<input type="text" name="name" placeholder="Enter Your Name">		
	
<input type="email" name="email" placeholder="Enter your email" required>			
<button class="sbtn">Submit</button>			
</form>	
2.create new php code in form_submit.php for submit the data to where you want
<?php 
$name=$_POST['name'];
$email=$_POST['email'];
echo "The data is available";
?>

3. create ajax script for auto submitting the form

var frm = $('#form');
frm.submit(function (e) {
	e.preventDefault();
	$.ajax({
		type: frm.attr('method'),
		url: frm.attr('action'),
		data: frm.serialize(),
		success: function (data) {
			console.log('Submission was successful.');
			console.log(data);
//you will get message :The data is available
					},
		error: function (data) {
			console.log('An error occurred.');
			console.log(data);
					},
		});
});

Completed

Leave a Reply

Your email address will not be published.

You may use these <abbr title="HyperText Markup Language">html</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

%d bloggers like this: