3
I’m developing a website where on the main page it has a section with a form. This form is for registering emails for sending newsletters. The other pages of the site also have other forms and all work correctly, except this main page.
HOW IT WORKS?
It is the simplest way possible. There are 2 input fields (Name and Email) and 1 send button as you can see below.
<section id="newsletter" class="newsletter bg-newsletter text-center">
<div class="container-fluid">
<div class="form-inline">
<span class="newsletter-text">RECEBA NEWSLETTERS</span>
<form id="formNewsLetter" accept-charset="utf-8">
<input type="hidden" name="Acao" value="NewsLetter">
<input type="hidden" name="adm_id" value="200">
<input type="text" class="form-control form-inline" name="nome" value="" placeholder="Nome">
<input type="email" class="form-control form-inline" name="email" value="" placeholder="E-mail">
<button type="submit" class="btn-newsletter">ENVIAR</button>
</form>
</div>
</div>
</section>
By clicking on "Send", Submit is activated and has a function javascript/jquery
which processes the data through AJAX
. From then on, the data arrives in the archive .PHP
and then the sending procedure works. See the code javascript
down below:
$("#formNewsLetter").submit(function(event){
event.preventDefault();
var data = $("#formNewsLetter").serialize();
$.ajax({
url: 'https://urldosite.com/sistema/Funcoes.php',
type: 'POST',
data: data,
success: function(response){
if(response.indexOf("alert-success") > -1){
$("#formNewsLetter").html("<br><div class='alert alert-success'><strong>SUCESSO!</strong> Email cadastrado com êxito!</div>");
} else {
console.log(response);
}
}
})
.done(function() {
console.log("success");
})
.fail(function() {
console.log("error");
})
.always(function() {
console.log("complete");
});
});
WHAT IS THE PROBLEM?
The problem here is when the data "exit the form to be processed in javascript code". When I click on "Send", the page redirects and the form data is there in the URL
of the website, as below:
http://urldositeprincipal.com.br/?Acao=NewsLetter&adm_id=200&nome=Lougans&email=lougans99%40mail.com
OBS: This link is from a test I did and sent, so it appears on URL
browser;
WHAT I HAVE TRIED
- Send through the
method
and ofaction
of<form>
(works straight);
Look at devConsole (shows no errors apparently);
WHAT I AM SURE IS NOT THE PROBLEM
- No problem in the file
.PHP
who sends the e-mail;
That’s it. I need your help to understand what’s going on. I’ve been through something like this before but the error was very clear, but it seems that it happened out of nowhere. Until Wednesday it was working properly and now it started to occur only in this form. The rest is fully functional.
NOTE: I would love the answers to be in part guidelines on the problem identified as well (this may come later and if you feel comfortable in doing so) an explanation of this behavior (I know it is between my page HTML
and the Javascript
.
If you need any other relevant information to help you understand my problem, I will be happy to edit the question and add this data.
My THANK YOU VERY MUCH to all who help me in solving these problems of mine!!!
This behavior is unusual, since you are using preventDefault to cancel the form Ubmit. There must be another code influencing this.
– Sam
@Sam yeah. I thought the same thing too. only part I haven’t touched is in the
success:
– Lougans de Matos
But it’s like I said: it must be something else. If you create a page only with this code then you will see that it does not happen.
– Sam