hi guys,
i'm created a form mail script, doing quite good.
just that, after i enter certain value and hit submit button, it checks for error. if error found, it returns to the page but a blank page. i'll have to start keying in the value again.
is there a way to retain the value after error checking?
it's a one page form mail.
appreciate if you could help me out, thanks.
PHP Code:
<?php session_start();
// Function to display form function showForm($errorName=false,$errorEmail=false,$errorSubj=false,$errorMesg=false,$errorSec=false) { if ($errorName) $errorTextName = 'Please enter a valid name.'; if ($errorEmail) $errorTextEmail = "Please enter a valid email address."; if ($errorSubj) $errorTextSubj = "Please enter a valid subject."; if ($errorMesg) $errorTextMesg = "Please leave a longer message."; if ($errorSec) $errorTextSec = "Wrong security code.";
require("top.php"); echo '<title>Wanna contact me??</title>'; require("header.php"); echo '</head><body><div id="maincontent"><form action="contact.php" method="post">';
// Display name field an error if needed echo 'Name : <input type="text" name="name" /><br />'; if ($errorName) echo "$errorTextName<br />";
// Display email field an error if needed echo 'Email Address : <input type="text" name="email" /><br />'; if ($errorEmail) echo "$errorTextEmail<br />";
// Display subject field an error if needed echo 'Subject : <input type="text" name="subj" /><br />'; if ($errorSubj) echo "$errorTextSubj<br />";
// Display message field an error if needed echo 'Message : <textarea name="mesg" rows="5" cols="20"></textarea><br />'; if ($errorMesg) echo "$errorTextMesg<br />";
echo '<p><img src="contact/CaptchaSecurityImages.php" width="120" height="40" alt="security code" /></p><p><input type="text" name="security_code" /></p>'; if ($errorSec) echo "$errorTextSec<br />";
echo '<p><input type="hidden" name="contact_ip" value="' . $_SERVER['REMOTE_ADDR'] . '" /><input type="submit" name="SubmitForm" value="Send" /> <input type="reset" /></p>'; echo '</form>'; require("footer.php"); }
if (!isset($_POST['SubmitForm'])) { showForm(); } else { //Init error variables
$errorName = false; $errorEmail = false; $errorSubj = false; $errorMesg = false; $errorSec = false;
$name = htmlspecialchars(isset($_POST['name']) ? trim($_POST['name']) : ''); $email = htmlspecialchars(isset($_POST['email']) ? trim($_POST['email']) : ''); $subj = htmlspecialchars(isset($_POST['subj']) ? trim($_POST['subj']) : ''); $mesg = htmlspecialchars(isset($_POST['mesg']) ? trim($_POST['mesg']) : ''); $ip = $_POST['contact_ip'];
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) $errorEmail = true; if (strlen($name)<3) $errorName = true; if (strlen($subj)<3) $errorSubj = true; if (strlen($mesg)<10) $errorMesg = true; if ( ($_SESSION['security_code']!=$_POST['security_code']) && (empty($_POST['security_code'])) ) $errorSec = true;
// Display the form again as there was an error if ($errorName || $errorEmail || $errorSubj || $errorMesg || $errorSec) { showForm($errorName,$errorEmail,$errorSubj,$errorMesg,$errorSec); } else { $from_name = "webmaster"; $from_email = "email@domain.com"; $mail_header = 'From: "' . $from_name . '" <' . $from_email . '>'; $mail_body = "Someone sent you an email from the website\nIt was submitted on " . date(DATE_RFC822) . "
--------------------------------------------------------- Name: " . $name . " Email Address: " . $email . " Subject: " . $subj . " Message: " . $mesg . " ---------------------------------------------------------
Remote IP: " . $ip;
$mail_subj = "Feedback Form"; if (mail($from_email,$mail_subj,$mail_body,$mail_header)) { echo '<h2>Message Successfully Sent</h2><p>Thank You! Your message has been successfully sent.</p><p>Please click <a href="home.php">here</a> to continue.</p>'; require("footer.php"); } else { echo '<h2>Problem Encountered</h2>There was a problem sending the mail. Please check that you filled in the form correctly by clicking <a href="contact.php">here</a>'; require("footer.php"); } } } ?>
|