I have written this code so that I can make a multi-step form and load and submit the data through AJAX.
The code is running well for the first form submission, but on the second attempt the page reloads itself. Can someone help me?
$(document).ready(function() {
jQuery.ajax({
type: "post",
url: 'index.php'
}).done(function(msg) {
$('#main-container').html(msg);
index();
})
function index() {
$('#myForm').submit(function(e) {
e.preventDefault();
var formData = $('#main-container').find('form').serialize();
console.log(formData);
jQuery.ajax({
type: "POST",
url: 'index.php',
data: formData
}).done(function(msg2) {
console.log(msg2)
$('#main-container').html(msg2);
});
});
}
});