Reputation: 57
I am trying to clear up the form after submission. I am not sure what I am missing. I will attach the PHP code below. This is my first time coding a contact form or anything using PHP, so try to bear with me. Thank you guys in advance!
Sorry thought I had attached the code.
<?php
if(isset($_POST['email'])) {
// EMAIL AND SUBJECT
$email_to = "##@##.com";
$email_subject = "New form submission";
function died($error) {
// your error code can go here
echo "<span style='color: red;' /><center> We're sorry, but there's errors found with the form you submitted.<br /><br /></center></span>";
echo $error."<br /><br />";
echo '<span style="color: red;" /><center>Please go back and fix these errors.<br /><br /></center></span>';
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.</center></span>');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$message = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= '<span style="color: red;" /><center>The Email Address you entered does not appear to be valid.<br /></center></span>';
}
$string_exp = "/^[0-9.-]+$/";
if(!preg_match($string_exp,$telephone) > 10) {
$error_message .= '<span style="color: red;" /><center>The Telephone number you entered does not appear to be valid.<br /></center></span>';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= '<span style="color: red;" /><center>The First Name you entered does not appear to be valid.<br /></center></span>';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= '<span style="color: red;" /><center>The Last Name you entered does not appear to be valid.<br /></center></span>';
}
if(strlen($message) < 10) {
$error_message .= '<span style="color: red;" /><center>The Message you typed seems to be too short, add more words, please.</center></span><br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- The success html below -->
<span style="color: green;"><center>Thank you for contacting us. We will be in touch with you very soon.</center></span>
<?php
}
die();
?>
Upvotes: 0
Views: 5936
Reputation: 1638
You could have one page with the form and all the information, like contact.php, but submit the data through the form to another page, like,
Then on process_page.php, once you have processed the data, redirect the user back to the original page with query string attached to it, like
header('Location: contact.php?status=mail%20sent%20successfully');
and display the query string through echo $_GET['status']
or create alternative message / layout based on that status. Don't forget to handle cases where $_GET is set, but empty, too!
This is a bit nicer, because it separates your processing logic from the interactive part of the website.
Upvotes: 1
Reputation: 351
All you need to do is reload the form like you did originally. I generally create a function that displays the form. When the first part of the script detects whether the form was submitted. If it was not, then you display the form. If it was, then you process the input THEN display the form.
Here's some pseudo-ish code to help out.
<?php
function display_form () {
// display your form here
// For my sample, I assume a clear button and a submit button
}
if (isset($_POST['submit'])) { // Submit button was clicked
// put your form processing code here
} else { // Form was not submitted or 'clear' was clicked
display_form();
}
?>
Upvotes: 2