Jump to content

Displaying Form Errors but avoid clearing already-filled data


codeline

Recommended Posts

I've got a basic form setup on my site that requires the user to fill out the required fields. When one of the fields isn't filled out, the error message for that specific input area is displayed, etc. However, all the information from the form that the user filled out is removed..

 

I want the user to be able to fill out the form, hit submit, and if any errors, show the specific error but also keep the input boxes populated with the data the user filled out so he/she does not have to re type everything.

 

if(!empty($_POST['submitFeature']))
{
    // set variables
    $featurename = mysql_real_escape_string($_POST['featurename']);
    $name = mysql_real_escape_string($_POST['name']);
    $email = mysql_real_escape_string($_POST['email']);
    $email2 = mysql_real_escape_string($_POST['email2']);
    $age = mysql_real_escape_string($_POST['age']);
    $city = mysql_real_escape_string($_POST['city']);
    $state = mysql_real_escape_string($_POST['state']);
    $src = $_FILES['featureupload']['tmp_name'];
    $featuresize = $_FILES['featureupload']['size'];
    $limitsize = 3000000;
        


    if(!empty($featurename) && !empty($name) && !empty($email) && !empty($email2) &&  !empty($city) &&  !empty($state) && ($email == $email2) && !empty($_FILES['featureupload']['tmp_name']) && ($featuresize < $limitsize))
    {

    // IF ALL IS CORRECT, SUBMIT INFO 
    
    } else {
        print '
        <ul class="errorlist">
            <li class="alert">Please fill out the required fields.</li>
        ';
    
        if (empty($name))
        {
            echo '	<li>* Full Name</li>' . "\n";
            $errorname = 'TRUE';	
        } if (empty($email))
        {
            echo '	<li>* Email</li>' . "\n";
            $erroremail = 'TRUE';
        } 
        print '
        </ul>
        ';
    }
    // 1 - B. END REQUIRED FIELDS ERROR CODES
    
}
?>

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
    <div style="float: left;">
        <span class="copy-answer">Your Information</span>
    
        <div class="formSec"><label for="name" class="required">Full Name: <?php if(isset($errorname)){echo '<span class="error">*<span>';}?></label>
        <input type="text" name="name" id="name" value="" maxlength="25" /></div>
        
        <div class="formSec"><label for="email" class="required">Email: <?php if(isset($erroremail)){echo '<span class="error">*<span>';}?></label>
        <input type="text" name="email" id="email" value="" /></div>
      
    <input class="submit" type="submit" name="submitFeature" value="Submit Your Feature" />
</form>

What you're asking about is making the form "sticky". For each of the form fields, if the $_POST['field_name'] is not empty, echo it as the value= attribute.

 

<input type="text" name="email" value="<?php if( !empty(trim($_POST['email'])) ) { echo $_POST['email']; } ?>">

 

Hmm, doesn't seem to be working. I've filled the "value" attribute with various options ($_POST['name'], $name) and whenever I fill it out and submit with incorrect info or required fields empty, the form resets all previous entered data.

 

Here's the code I have:

 

if(!empty($_POST['submitFeature']))
{
    $name = mysql_real_escape_string($_POST['name']);
    $email = mysql_real_escape_string($_POST['email']);
        


    if(!empty($name) && !empty($email))
    {

    // IF ALL IS CORRECT, SUBMIT INFO 
    
    } else {
        print '
        <ul class="errorlist">
            <li class="alert">Please fill out the required fields.</li>
        ';
    
        if (empty($name))
        {
            echo '<li>* Full Name</li>' . "\n";
            $errorname = 'TRUE';
	} if (empty($email))
        {
            echo '<li>* Email</li>' . "\n";
            $erroremail = 'TRUE';
        } 
        print '
        </ul>
        ';
    }
    // 1 - B. END REQUIRED FIELDS ERROR CODES
    
}
?>

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
    <div style="float: left;">
        <span class="copy-answer">Your Information</span>
    
        <div class="formSec"><label for="name" class="required">Full Name: <?php if(isset($errorname)){echo '<span class="error">*<span>';}?></label>
        <input type="text" name="name" id="name" value="" maxlength="25" value="<?php if(!empty($_POST['name'])) { echo $_POST['name']; } ?>" /></div>
        
        <div class="formSec"><label for="email" class="required">Email: <?php if(isset($erroremail)){echo '<span class="error">*<span>';}?></label>
        <input type="text" name="email" id="email" value="" /></div>
      
    <input class="submit" type="submit" name="submitFeature" value="Submit Your Feature" />
</form>

You left the original empty value="" attributes in the tags also. Just remove those and you should be all fixed up.

 

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
    <div style="float: left;">
        <span class="copy-answer">Your Information</span>

        <div class="formSec"><label for="name" class="required">Full Name: <?php if(isset($errorname)){echo '<span class="error">*<span>';}?></label>
        <input type="text" name="name" id="name" maxlength="25" value="<?php if(!empty($_POST['name'])) { echo $_POST['name']; } ?>" /></div>

        <div class="formSec"><label for="email" class="required">Email: <?php if(isset($erroremail)){echo '<span class="error">*<span>';}?></label>
        <input type="text" name="email" id="email" value="<?php if(!empty($_POST['email'])) { echo $_POST['email']; } ?>" /></div>

    <input class="submit" type="submit" name="submitFeature" value="Submit Your Feature" />
</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.