forms saving fields so that user does not have to type again

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
djcrisp
Forum Newbie
Posts: 6
Joined: Mon Dec 01, 2008 6:25 pm

forms saving fields so that user does not have to type again

Post by djcrisp »

ok, i have this issue when user types in information in the form fields. but when if there is error with one of the fields when user types it wrong. then the all other fields go blank when error occurs. so if user type something wrong in one field. the other fields should keep the data unless if they are typed wrong as well. saving fields with php. any help is appreciated. i need help. please, help. thanks in advance :-)

Code: Select all

 
<?php
// Function to display form
function showForm($errorName=false,$errorEmail=false,$errorAddress=false,$errorCity=false,$errorLastName,$errorState,$errorZip,$errorPhone){
        
       if ($errorName) $errorTextName = "Please enter your first name!";
       if ($errorLastName) $errorTextLastName = "Please enter your last name!";         
       if ($errorCity) $errorTextCity = "Please enter a city!";
       if ($errorEmail) $errorTextEmail = "Please enter a valid email address!";
       if ($errorAddress) $errorTextAddress = "Please enter a address!";
       if ($errorState) $errorTextState = "Please enter name of state!";
       if ($errorZip) $errorTextZip = "Please enter zip code!";
       if ($errorPhone) $errorTextPhone = "Please enter phone number!";
         
      
         echo '<form action="index.php" method="POST"><table>';
         
        // Display firstname field an error if needed
        echo '<tr><td>First Name:</td><td><input type="text" name="name"></td></tr>';
        if ($errorName) echo "<tr><td colspan='2'>$errorTextName</td></tr>";
 
        // Display lastname field an error if needed
        echo '<tr><td>Last Name:</td><td><input type="text" name="lastname"></td></tr>';
        if ($errorLastName) echo "<tr><td colspan='2'>$errorTextLastName</td></tr>";
 
        //display address field an error if needed
        echo ' <tr><td>Address:</td><td><input type="text" name="address"></td></tr>';
        if ($errorAddress) echo "<tr><td colspan='2'>$errorTextAddress</td></tr>";
 
        // Display City field an error if needed
        echo '<tr><td>City:</td><td><input type="text" name="city"></td></tr>';
        if ($errorCity) echo "<tr><td colspan='2'>$errorTextCity</td></tr>";
 
 
        // Display State field an error if needed
        echo '<tr><td>State:</td><td><input type="text" name="state"></td></tr>';
        if ($errorState) echo "<tr><td colspan='2'>$errorTextState</td></tr>";
 
        // Display zip field an error if needed
        echo '<tr><td>Zip:</td><td><input type="text" name="zip"></td></tr>';
        if ($errorZip) echo "<tr><td colspan='2'>$errorTextZip</td></tr>";
 
        // Display phone number field an error if needed
        echo '<tr><td>Phone:</td><td><input type="text" name="phone"></td></tr>';
        if ($errorPhone) echo "<tr><td colspan='2'>$errorTextPhone</td></tr>";
 
        // Display email field an error if needed
        echo '<tr><td>Email:</td><td><input type="text" name="email"></td></tr>';
        if ($errorEmail) echo "<tr><td colspan='2'>$errorTextEmail</td></tr>";
 
       
        
     
       
 echo '<tr><td><input type="submit" name="SubmitForm" value="Send"></td></tr>';
        echo '<form>';
        }
       
     
 
  if (!isset($_POST['SubmitForm'])) {
        showForm();
        } else {
        //Init error variables
        $errorName = false;
        $errorLastName = false;
        $errorAddress = false;
        $errorCity = false;  
        $errorState = false;
        $errorZip = false;           
        $errorPhone = false; 
        $errorEmail = false;
       
 
 
 
 
        
  $name = isset($_POST['name']) ? trim($_POST['name']) : '';
  $lastname = isset($_POST['lastname']) ? trim($_POST['lastname']) : ''; 
  $address = isset($_POST['address']) ? trim($_POST['address']) : '';  
  $city = isset($_POST['city']) ? trim($_POST['city']) : '';    
  $state = isset($_POST['state']) ? trim($_POST['state']) : ''; 
  $zip = isset($_POST['zip']) ? trim($_POST['zip']) : ''; 
  $phone = isset($_POST['phone']) ? trim($_POST['phone']) : ''; 
  $email = isset($_POST['email']) ? trim($_POST['email']) : '';
             
  
         
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($lastname)<3) $errorLastName = true;
if (strlen($address)<1) $errorAddress = true;
if (strlen($city)<1) $errorCity = true;
 
if (strlen($state)<1) $errorState = true;
 
if (!eregi("^[0-9]{5}(\-[0-9]{4})?$", $zip)) $errorZip = true;
 
if (!eregi("^[0-9]{10}$", $phone)) $errorPhone = true;
 
      
      // Display the form again as there was an error
if ($errorName || $errorEmail || $errorAddress || $errorCity || $errorLastName || $errorState || $errorZip || $errorPhone)
 {
        showForm($errorName,$errorEmail,$errorAddress,$errorCity,$errorLastName,$errorState,$errorZip,$errorPhone);
        } else {
      
echo header('Location: index.html');
        }
     }
?> 
 
 
 
 
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: forms saving fields so that user does not have to type again

Post by pbs »

Try using below given code, replace you form code with this

echo '<form action="index.php" method="POST"><table>';

// Display firstname field an error if needed
echo '<tr><td>First Name:</td><td><input type="text" name="name" value="<?=trim($_POST[name])?>"></td></tr>';
if ($errorName) echo "<tr><td colspan='2'>$errorTextName</td></tr>";

// Display lastname field an error if needed
echo '<tr><td>Last Name:</td><td><input type="text" name="lastname" value="<?=trim($_POST[lastname])?>"></td></tr>';
if ($errorLastName) echo "<tr><td colspan='2'>$errorTextLastName</td></tr>";

//display address field an error if needed
echo ' <tr><td>Address:</td><td><input type="text" name="address" value="<?=trim($_POST[address])?>"></td></tr>';
if ($errorAddress) echo "<tr><td colspan='2'>$errorTextAddress</td></tr>";

// Display City field an error if needed
echo '<tr><td>City:</td><td><input type="text" name="city" value="<?=trim($_POST[city])?>"></td></tr>';
if ($errorCity) echo "<tr><td colspan='2'>$errorTextCity</td></tr>";


// Display State field an error if needed
echo '<tr><td>State:</td><td><input type="text" name="state" value="<?=trim($_POST[state])?>"></td></tr>';
if ($errorState) echo "<tr><td colspan='2'>$errorTextState</td></tr>";

// Display zip field an error if needed
echo '<tr><td>Zip:</td><td><input type="text" name="zip" value="<?=trim($_POST[zip])?>"></td></tr>';
if ($errorZip) echo "<tr><td colspan='2'>$errorTextZip</td></tr>";

// Display phone number field an error if needed
echo '<tr><td>Phone:</td><td><input type="text" name="phone" value="<?=trim($_POST[phone])?>"></td></tr>';
if ($errorPhone) echo "<tr><td colspan='2'>$errorTextPhone</td></tr>";

// Display email field an error if needed
echo '<tr><td>Email:</td><td><input type="text" name="email" value="<?=trim($_POST[email])?>"></td></tr>';
if ($errorEmail) echo "<tr><td colspan='2'>$errorTextEmail</td></tr>";





echo '<tr><td><input type="submit" name="SubmitForm" value="Send"></td></tr>';
echo '<form>';
Post Reply