I'm a beginner in programming and I have a little problem here. I have already done a good search here in stackoverflow, both in Portuguese and in English, but I could not adpatar or put a solution in practice here. I apologize if the code does hurt in your eye, but start is beginning, hehehe.
It is as follows, it has a form that calls the "contact.php" code with action and when it clicks to send, it performs 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 happens that after sending, it redirects to the page "contato.php" and I wanted it to do nothing, only stay on the page where it appears a text saying that the message was sent. The solutions I saw consisted of putting something like:
header("url: contato.html?sent=true");
But if I try this, it ends up returning this error for me:
Can not modify header information - headers already sent by (output > started at /Applications/MAMP/htdocs/Contato_Colab/contact.php:1)
Other solutions were in the use of AJAX, but were directed to those who already knew how it works and I do not know anything about it yet. That's my question, can you prevent this redirect without using AJAX? Or will I have to learn urgently?
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>
contact.php
<?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;
}
}