Return after mail function call

Good day,

I am a very novel programmer, just making my first website for a new idea we are starting.

From “contact us” page, we have a form to get information, and when press send button, send_email.php function is called.
All works fine, I receive the e-mail generated by the function.

My question is, how can i return to my website from the PHP mail() function?

In PHP code I added a line:

echo “<div style=‘position:absolute; top:215px; left:200px; color:#FF4F10’><br><br><h2>Your request has been successfully sent</h2></div>”;

But the message is showed in a blank page.
I have created includes in my HTML website, in order to avoid repeating code. I have includes for header, menu and footer sections.

Id like the message generated after mail() function sent the e-mail is showed in a web page with these three includes.

Thanks a lot!!!

Can you post the code of send_email.php? (preferably between [php ] tags)

That way we can see what’s happening in your script.

Of course:

<?

$cuerpo = "Formulario enviado\

";
$cuerpo .= "Nombre: " . $_POST[‘name’] . "
";
$cuerpo .= “E-mail” . $_POST[‘email’] . "
";
$cuerpo .= "Asunto: " . $_POST[‘subject’] . "
";
$cuerpo .= "Comentario: " . $_POST[‘comment’] . "
";

//mando el correo... 
mail("arturo.ramirezd@gmail.com","Formulario recibido desde website Libromanía", $cuerpo);

//doy las gracias por el envío 
echo  "&lt;div style='position:absolute; top:215px; left:200px; color:#FF4F10'&gt;&lt;br&gt;&lt;br&gt;&lt;h2&gt;Su Solicitud ha sido Enviada Satisfactoriamente&lt;/h2&gt;&lt;/div&gt;"; 

?>

and it is called from:
http://www.libromania.cl/spanish/contactus_spanish.shtml
when press “Enviar” button.

Thanks!!!

There’s a couple of things you could do:

  1. have send_mail.php show an entire page (so also include your header and footer includes)
  2. redirect to a different page with the ‘Su Solicitud ha sido Enviada Satisfactoriamente’ message

The second options is probably the best, as it has the added benefit of not sending a new mail out again if someone presses F5


$cuerpo = "Formulario enviado\
";
$cuerpo .= "Nombre: " . $_POST['name'] . "\
";
$cuerpo .= "E-mail" . $_POST['email'] . "\
";
$cuerpo .= "Asunto: " . $_POST['subject'] . "\
";
$cuerpo .= "Comentario: " . $_POST['comment'] . "\
";


//mando el correo...
mail("arturo.ramirezd@gmail.com","Formulario recibido desde website Libromanía", $cuerpo);

header('Location: http://www.libromania.cl/spanish/gracias.html');
die();

And then you create a new page gracias.html which contains the ‘Su Solicitud ha sido Enviada Satisfactoriamente’ message.

Great!!! I will check and post results!!!

Hi Immerse,

Thanks a lot for your answer!!
I applied your instructions and now it is working properly.
You can see here directly:
http://www.libromania.cl/spanish/answer_spanish.shtml

Or after send me a message in:
http://www.libromania.cl/spanish/contactus_spanish.shtml

Thanks!!!

Hi Immerse,

Now the next question. I hope you can help me again…

Today, when press “Enviar” button, a message is generated, no matterthe information in the form. How man make the four fields mandatory? It is, not sending the e-mail if all four fields are not filled.

Here the script again:

<?
$message_body = "Formulario enviado
";
$message_body .= "Nombre: " . $_POST[‘name’] . "
";
$message_body .= "E-mail: " . $_POST[‘email’] . "
";
$message_body .= "Asunto: " . $_POST[‘subject’] . "
";
$message_body .= "Comentario: " . $_POST[‘comment’] . "
";

mail("arturo.ramirezd@gmail.com","Formulario recibido desde website Libromanía", $message_body);

header('Location: http://www.libromania.cl/spanish/answer_spanish.shtml');
die(); 

?>

Thanks!!!

You can check the contents of the variables like this:


if(empty($_POST['name'])) {
    $error = 'Please enter your name.';
}

This uses the function [fphp]empty/fphpto test if the variable $_POST[‘name’] is, um, empty.

Now you need to figure out how to display the errors that people might get :slight_smile:

Great, thanks. How can I give some format to that error message? I mean position, font size, color …

Thanks!!!

Wrap the error in a span tag, and style that ie:


$error = '<span style="font: red Times 12pt;font-weight: bold;">Please enter your name.</span>';

wow, great move. Is it possible to put these styles in a CSS file? (considering this is a PHP script and not an HTML file?)

Thanks!!!

sure is!
style.css


.error {
   font: red Times 12pt;
   font-weight: bold;
}

php file:


$error = '<span class="error">Please enter your name.</span>';

You’ll just have to remember to include the style.css even when displaying errors. Usually done with custom error handling instead of using die($error); for ex.

Super!!! I’ll check and post results!!!

Im afraid I am not being able to make it works.
When press “send” button, it jumps to the page where thanks for the successful message. There is no error message.
You can see it in:
http://www.libromania.cl/spanish/contactus_spanish.shtml

The PHP function is:

<?
$message_body = "Formulario enviado
";
$message_body .= "Nombre: " . $_POST[‘name’] . "
";
if(empty($_POST[‘name’])){
$error = ‘<span class=“error”>Please enter your name.</span>’;
}
$message_body .= "E-mail: " . $_POST[‘email’] . "
";
$message_body .= "Asunto: " . $_POST[‘subject’] . "
";
$message_body .= "Comentario: " . $_POST[‘comment’] . "
";

mail("arturo.ramirezd@gmail.com","Formulario recibido desde website Libromanía", $message_body);


header('Location: http://www.libromania.cl/spanish/answer_spanish.shtml');
die(); 

?>

And in styles.css file I added:

.error{
font-size: 150%;
color: red;
font-weight: bold;
}

Thanks!!!