Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Form field to retain value after error checking
Old 03-22-2009, 06:02 AM Form field to retain value after error checking
Experienced Talker

Posts: 38
Trades: 0
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" />&nbsp;&nbsp;&nbsp;<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");
        }
    }
}
?>
superkingkong is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-22-2009, 07:47 AM Re: Form field to retain value after error checking
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Change the echo lines like this:

PHP Code:
echo 'Name : <input type="text" name="name" value="'.htmlspecialchars($_POST['name']).'"/><br />'
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 03-22-2009, 08:10 AM Re: Form field to retain value after error checking
Experienced Talker

Posts: 38
Trades: 0
thanks a lot for the help
superkingkong is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Form field to retain value after error checking
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.11544 seconds with 12 queries