So basically, I'm making a website with a form inside a modal. After that form submits the data to script.php, whatever script.php sends back is appended to the modal body like so:
// Function to submit data to php
$("#formModificarArticulo").on("submit", function(e) {
var postData = $(this).serializeArray();
var formURL = $(this).attr("action");
$.ajax({
url: formURL,
type: "POST",
data: postData,
success: function(data, textStatus, jqXHR) {
$('#divModificarArticulo .modal-header .modal-title').html("Resultado");
$('#divModificarArticulo .modal-body').html(data);
$("#submitFormModificar").remove();
reloadDataTable();
},
error: function(jqXHR, status, error) {
console.log(status + ": " + error);
}
});
e.preventDefault();
});
$("#submitFormModificar").on('click', function() {
console.log("Submit presionado");
$("#formModificarArticulo").submit();
});
So, basically, when the user sends data using the form inside the modal, the modal changes to show something along the lines of "Data sent to server successfully".
After the result from script.php is showed to the user and the user closes it, the modal is supposed to revert back to its original state with the form. This is the function where that magic happens:
// Function to reset modal
(function(){
var template = null;
$('#divModificarArticulo').on('show.bs.modal', function (event) {
if (template == null) {
template = $(this).html();
} else {
$(this).html(template);
}
$('#divModificarArticulo').modal({ show: false});
})
})();
The thing is, I can send data pressing the Submit button on the form, the modal brings me the back the results from the server and when I open the modal for a second time the form is back to its original state. For some reason, however, when I press the Submit button for a second time, it doesn't do anything. It's like it's not even pressed.
I'd really appreciate if someone could help me out.