Ajax Form Submit

Important syntex :

serialize() : This is used for to take form data

Basic Ajax form submit below is javascript code:

<script>
$(document).ready(function(){
$('#btn').click(function(){
$.ajax({
url:'post.php',
type: 'post',
data: $('#forma').serialize(),
success: function(result){
$('#err_msg').html(result)
},
});
});
});

</script>

Below code is HTML
<form id="forma" method = "post" name = "contectform">
<input type="text" name="name" id="name" placeholder="name" required>
<input type="text" name="phone" id ="phone" placeholder="Phone" required>

<input type="submit" id="btn" value = "Submit" >
<div id="err_msg"></div>
</form> 



Use below code in post.php
<?php
echo 'This is my post page';
print_r($_POST);
?>

Leave a Reply

Your email address will not be published. Required fields are marked *