I am trying to use jQuery ajax to collect data from a form and then print the data using print_r.
My index.php looks like this:
<!DOCTYPE html>
<html>
<head>
<title>CRUD application with AJAX</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#insert').on("click", function(event){
event.preventDefault();
$.ajax({
url: "insert.php",
method: "post",// here we pass the form methos
data: $('form').serialize(), // data is sent from here
dataType: "text",
success: function(strDate){
$('#message').text(strDate)
}
})
})
})
</script>
</head>
<body>
<p id="message"></p>
<form method="post" id="insert_form">
<table cellpadding="5" cellspacing="5">
<tr>
<th>Enter Name</th><td><input type="text" id="name" name="name"></td>
</tr>
<tr>
<th>Enter Email</th><td><input type="text" id="email" name="email"></td>
</tr>
<tr>
<th>Enter Contact</th><td><input type="text" id="contact" name="contact"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="button" id="insert" name="insert" value="Insert"></td>
</tr>
</table>
</form>
</body>
</html>
In my ajax function I referred to insert.php so that looks like this:
<?php
print_r($_POST);
?>