Good afternoon, I have a form and I want it when the submit
button is clicked it does not reload the page for sending the post I placed return false;
in function submit
of jQuery
but did not roll .. .
$("#form-anuncio-edita-texto").submit(function () {
if ($("#edita-anuncio-texto").val() !== "") {
var formData = new FormData(this);
$("#page-load").toggleClass('sk-loading');
$("#anuncio-edita-text-alert").hide();
$('#modal-anuncios-3').modal('toggle');
var request = $.ajax({
url: "modulos/paginas/" + identificador + "/process/updateMedia.php",
method: "POST",
data: formData
});
request.done(function (data) {
$("#page-load").toggleClass('sk-loading');
alert(data);
});
request.fail(function () {
$("#page-load").toggleClass('sk-loading');
toastNotification("<a href='index.php'>A página não pode ser carregada, se este erro persistir sujerimos que atualize a página ou clique <strong>AQUI</strong> e tente novamente.</a>", "Erro no Carregamento...", 15000, "error");
});
} else {
$("#anuncio-edita-text-alert").show();
}
return false;
});
When the field is empty it enters else
and the page does not reload, but when I enter any value it reloads.
WARNING: This example is with a text field that would be easily resolved using the click
function of jQuery
storing content in var
and putting it in data: {texto: vartexto}
, but I can not use it because in that form has a FILE
field where I will send a file via AJAX.