Jump to content
Larry Ullman's Book Forums

Chapter 10 Email Of Html Form


Recommended Posts

Hi,

 

I am a beginner at PHP and programming in general but have needed to have a working contact form on my business website. This is more than likely very easy but I just cannot get what I need working.

 

I have been trying to implement the script to send an email from a HTML form. The script as written in the book prints a message on submission of the form but I want it to redirect to another page instead.

 

I have looked around for the solution but cannot figure out how to make this work. I believe that I need to use :

 

header('Location: applythanks.html');exit();I have put this into the code under the part that sends the message, which I do receive, but the page does not redirect. I have also tried putting it in other parts of the code but still do not get the desired result.The original script is as follows:

 

<?php # Script 10.1 - email.php

 

// Check for form submission:

if (isset($_POST['submitted'])) {

 

// Minimal form validation:

if (!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['comments']) ) {

 

// Create the body:

$body = "Name: {$_POST['name']}\n\nComments: {$_POST['comments']}";

 

// Make it no longer than 70 characters long:

$body = wordwrap($body, 70);

 

// Send the email:

mail('your_email@example.com', 'Contact Form Submission', $body, "From: {$_POST['email']}");

 

// Print a message:

echo '<p><em>Thank you for contacting me. I will reply some day.</em></p>';

 

// Clear $_POST (so that the form's not sticky):

$_POST = array();

 

} else {

echo '<p style="font-weight: bold; color: #C00">Please fill out the form completely.</p>';

}

 

} // End of main isset() IF.

 

// Create the HTML form:

?>

 

I would be very grateful if someone could help me with this, as am tearing my hair out!

 

Many thanks

Link to comment
Share on other sites

Check out the description of the PHP mail function on the following page:

 

http://php.net/manua...nction.mail.php

 

Make sure that your arguments are all valid, especially the format of the to and from email addresses. Also, because the mail function returns a Boolean value, you can use that as a test for redirecting the user. For example, redirect if the mail is successfully sent, otherwise, post some sort of error message, asking for some sort of confirmation:

 

if (mail($to, $subject, $message, $additional_headers)) {

 echo 'Mail sent. Redirecting...';

 header('Location: ' . $url);

 exit();

} else {

 echo 'Failed.';

}

Link to comment
Share on other sites

 Share

×
×
  • Create New...