0
I’m a beginner in programming and I have a little problem here. I have already done a good research here in stackoverflow, both in Portuguese and in English, but I could not adapt or put a solution in practice here. I apologize if the code hurts in your eye, but start is start, hehehe.
Here’s the thing, there’s a form that calls with action the code "contact.php" and when it clicks to send, it executes a function in javascript, a simple validation that checks if the required fields are filled with something and then the form is sent to an email. All this works very well, it turns out that after sending, it redirects to the page "contact.php" and I wanted her to do nothing, just stay on the page where appears a text saying that the message was sent. The solutions I saw were to put something like:
header("url: contato.html?sent=true");
But if I try this, it ends up returning this mistake to me:
Cannot Modify header information - headers already sent by (output >Started at /Applications/MAMP/htdocs/Contact_colab/contact.php:1)
Other solutions were in the use of AJAX, but were targeted to those who already knew how it works and I don’t know anything about it yet. There is my question, is there any way to prevent this redirection without using AJAX? Or I will have to learn emergentially?
Form
<form name="form_contato" action="contato.php" onsubmit="return validateForm()" method="post">
<input id="nome" name="nome" type="text" placeholder="Nome Completo*" class="celulaContato" >
<input name="telefone" type="text" placeholder="Telefone" class="celulaContatoESQ">
<input name="email" type="text" placeholder="E-mail*" class="celulaContato" >
<input name="site" type="text" placeholder="Website" class="celulaContatoESQ">
<input name="empresa" type="text" placeholder="Empresa" class="celulaContato">
<input name="cargo" type="text" placeholder="Cargo" class="celulaContatoESQ">
<input name="assunto" type="message" placeholder="Assunto*" class="celulaContatoASSUNTO" >
<textarea name="mensagem" rows="4" placeholder="Sua mensagem*" >
</textarea>
<input id="enviar" name="enviar" type="submit" value="Enviar">
<div id="check"><input name="newsletter" type="checkbox"> <p>Receber Newsletter</p></div>
php contact.
<?php
$nome = $_POST["nome"] ;
$telefone = $_POST["telefone"] ;
$email = $_POST["email"] ;
$site = $_POST["site"] ;
$empresa = $_POST["empresa"] ;
$cargo = $_POST["cargo"] ;
$assunto = $_POST["assunto"] ;
$mensagem = $_POST["mensagem"] ;
$from = "Formulário de Contato - Site Colab";
$to = "[email protected]";
$subject = "blablabla";
$body = "De = $nome\n E-mail: $email\n Website: $site\n Empresa: $empresa\n Cargo: $cargo\n Assunto: $assunto\n Mensagem:\n $mensagem" ;
if ($_POST['enviar']) {
mail ($to, $subject, $body, $from);
}
?>
validate form.
var x = document.forms["form_contato"]["nome"].value;
if (x == "") {
document.getElementById("invalido_form").innerHTML = "O campo nome deve ser preenchido";
return false
}
var x = document.forms["form_contato"]["email"].value;
if (x == "") {
document.getElementById("invalido_form").innerHTML = "O campo E-mail deve ser preenchido";
return false;
}
var x = document.forms["form_contato"]["assunto"].value;
if (x == "") {
document.getElementById("invalido_form").innerHTML = "O campo Assunto deve ser preenchido";
return false;
}
var x = document.forms["form_contato"]["mensagem"].value;
if (x == "") {
document.getElementById("invalido_form").innerHTML = "O campo Mensagem deve ser preenchido";
return false;
}
else {
document.getElementById("invalido_form").innerHTML = "Sua mensagem foi enviada!";
return true;
}
}
Where is the request in your javascript that you are doing ?
– NoobSaibot
I tried to put the latter to false, but then it does not send the email. Sorry, but I didn’t quite understand what you meant by the request in my javascript, what I have of javascript is inside <script> in html
– Lucas Maraal
Learn ajax urgently bro, it is very simple and very effective if you want to show a php code that I am using ajax
– RickPariz