Hi guys! I´m really new to PHP, but i'm involved in a small project. But i'm having an issue with the mail form. It was working, but as the user refreshed the page it would send the values of the variables again and again...
First i managed to auto refresh the page when submiting the values, and it worked. But that means that if the page has a lot of content it will tale always alot of time to refresh.
So i tried to split the code in two. With index.php and mess.php. index.php contais the form, wich than sends the values to mess.php, processing the values and returning to index.php. But i have validations working on mess.php and i want to show them on index.php when this page refreshes. But i can't seem to make it. I've tried everything and searched every post!I need some help please...
Here's the two pages:
index.php
This contains the form and the variable &sucesso, wich will be filled and displayed with the validation messages in variable &sucesso in mess.php.
<p class="copyright">© <?php echo date("Y"); ?> AroundField. Todos os direitos reservados. Design by <a href="http://www.ideal-line.pt">Ideal Line</a>.</p>
</div>
<p class="villaPT"><a>Villa Portucalle - Traditional Food & Arts Trading Company</a></p>
</div>
<div id="form">
<?=$sucesso?>
<h4>Deseja ser contactado(a)?</h4>
<form action="mess.php" method="post">
<p><label for="nome">Nome</label> <input type="text" name="nome" id="nome" value="<?=$_SESSION['nome']?>"/></p>
<p><label for="email">Email <span class="error">*</span></label> <input type="text" name="email" id="email" value="<?=$_SESSION['email']?>"/></p>
<p><textarea name="conteudo" id="conteudo"><?=$_SESSION['conteudo']?></textarea></p>
<p><input type="submit" name="enviar" value="enviar »" /></p>
<input type='hidden' value='1' name='submitted' />
</form>
</div>
</div>
</body>
</html>
mess.php
This contais the PHP with validation messages within a variable($sucesso)wich i need to send to $sucesso in index.php after validation
<?php
function is_email($email) {
eregi("^([0-9a-zA-Z]+)([.,_]([0-9a-zA-Z]+))*[@]([0-9a-zA-Z]+)([.-]([0-9a-zA-Z]+))*[.]([0-9a-zA-Z]){2}([0-9a-zA-Z])?$",$email,$match);
list($email_comp ,$login, $domain, $sufixies) = $match;
if ($email_comp == $email) {
return true;
} else {
return false;
}
}
// Prepare email to send
if ($_POST['submitted']) {
$para = "nuno.baixinho@epochmultimedia.com"; //info@aroundfield.pt
$assunto = "contacto via website\r\n";
$conteudo = $_POST['conteudo']."\r\n";
$conteudo .= $_POST['nome']."\r\n";
$conteudo .= $_POST['email']."\r\n";
$header='MIME-Version: 1.0' . "\r\n" . 'Content-type: text/plain; charset=UTF-8' . "\r\n" . 'From: Website <no-reply@localhost>' . "\r\n";
$_SESSION['nome'] = "";
$_SESSION['email'] = "";
$_SESSION['conteudo'] = "";
$_SESSION['nome'] = $_POST['nome'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['conteudo'] = $_POST['conteudo'];
// Validate email and required fields
if(!is_email($_POST['email']) || $_POST['email'] == "") {
$erro2 = 1;
$_SESSION['email'] = "";
$sucesso= "<p class='alert error'><strong>(!)</strong> Introduza um email válido..</p>";
header('Location: index.php');
}
if ($erro1 != 1 && $erro2 != 1) {
if (mail($para, $assunto, $conteudo, $header)) {
$sucesso = "<p class='alert confirm'>Mensagem enviada!</p>";
header('Location: index.php');
$_SESSION['nome'] = "";
$_SESSION['email'] = "";
$_SESSION['conteudo'] = "";
} else {
$sucesso = "<p class='alert error'><strong>(!)</strong> Ocorreram erros..</p>";
header('Location: index.php');
}
}
}
?>
Thanks!