Jump to content

HELP retain input values after submitting???


wblati

Recommended Posts

hi, can someone please help me retain the input values after the user gets an error message or has completed the form. and also is there a way of putting the errors messages next the corresponding input fields. thanks.

 

hi, can someone please help me retain the input values after the user gets an error message or has completed the form. and also is there a way of putting the errors messages next the corresponding input fields. thanks.

<html>
   <head>
   
   <style type=text/css>
input.blue {background-color: #0066FF; font-weight: bold; font-size: 12px; color: white;}
input.violet {background-color: #ccccff; font-size: 14px;}
textarea.violet {background-color: #ccccff; font-size: 14px;}
option.red {background-color: #cc0000; font-weight: bold; font-size: 14px; color: white;}
option.pink {background-color: #ffcccc;}
.style2 {	color: #990000;
font-weight: bold;
font-size: 36px;
font-family: Verdana, Arial, Helvetica, sans-serif;
}
.style5 {font-family: Arial, Helvetica, sans-serif; font-weight: bold; }
.style6 {font-family: Arial, Helvetica, sans-serif}
body {
background-color: #FFFFCC;
}
</style>
   
      <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
      <title>
Form Page: test      </title>
      <Style>
        BODY, P,TD{ font-family: Arial,Verdana,Helvetica, sans-serif; font-size: 10pt }
        A{font-family: Arial,Verdana,Helvetica, sans-serif;}
        B {	font-family : Arial, Helvetica, sans-serif;	font-size : 12px;	font-weight : bold;}
        .style2 {color: #990000;
font-weight: bold;
font-size: 36px;
font-family: Verdana, Arial, Helvetica, sans-serif;
}
      </Style>
      </script>
   </head>
<body>
<h2 align="center"><span class="style2">ONLINE MOVIE BOOKING</span></h2>


<div align="left">
  <?PHP // VALIDATIONS TAKEN php file formValidator.php (http://www.phpbuilder.com/columns/weiner20050831.php3?page=2)
require_once "formvalidator.php";
$show_form=true;

class MyValidator extends CustomValidator
{
function DoValidate(&$formars,&$error_hash)
{
        if(stristr($formars['Comments'],'http://'))
        {
            $error_hash['Comments']="No URLs allowed in comments";
            return false;
        }
	return true;
}
}

if(isset($_POST['Submit']))
{
    $validator = new FormValidator();

// TAKEN FROM http://www.html-form-guide.com/php-form/php-form-validation.html

// VALIDATION FOR NAME -------------------------------------------------------------------- 
    $validator->addValidation("NAME","req","Please enter a name");
$validator->addValidation("NAME","alpha_s","Please enter only letters");

// VALIDATION FOR EMAIL -------------------------------------------------------------------
$validator->addValidation("EMAIL","req","Please enter an email address");
    $validator->addValidation("EMAIL","email","Please enter a valid email address");

// VALIDATION FOR CARD NUMBER -------------------------------------------------------------
$validator->addValidation("CARD_NUMBER","req","Please enter a card number");
$validator->addValidation("CARD_NUMBER","numeric","Please enter only numbers");

// VALIDATION FOR CARD TYPE ---------------------------------------------------------------
$validator->addValidation("CARD_TYPE","dontselect=Please Choose","Please select a card type");

// VALIDATION FOR MOVIE -------------------------------------------------------------------
$validator->addValidation("MOVIE","dontselect=Please Choose","Please select a movie type");

// VALIDATION FOR DATE --------------------------------------------------------------------
$validator->addValidation("DATE","dontselect=Please Choose","Please select a date type");

// VALIDATION FOR TIME --------------------------------------------------------------------
$validator->addValidation("TIME","selectradio=TIME","Please select a time");

    $custom_validator = new MyValidator();
    $validator->AddCustomValidator($custom_validator);

    if($validator->ValidateForm())
    {
        echo "<h2>Validation Success!</h2>";
        $show_form=false;
    }
    else
    {
        echo "<B>Validation Errors:</B>";

        $error_hash = $validator->GetErrors();
        foreach($error_hash as $inpname => $inp_err)
        {
            echo "<p>$inpname : $inp_err</p>\n";
        }        
    }
}

if(true == $show_form)
{
?>
    
  </div>
<form name='test' method='POST' action='' accept-charset='UTF-8'>
  <div align="center">
<table width="444" border="1">
            <tr>
              <td><span class="style5">NAME ON CARD: </span></td>
               <td class='element_label'>
                  <input class="violet" type='text' name='NAME' size='30'>               
		   </td>
             </tr>
            <tr>
              <td><span class="style5">EMAIL: </span></td>
               <td class='element_label'>
                  <input class="violet" type='text' name='EMAIL' size='30'>               
		   </td>
             </tr>

		    <tr>
      <td><span class="style5">CARD_NUMBER: </span></td>
      <td><input class="violet" name="CARD_NUMBER" type='number' size='9'></td>
    </tr>
    <tr>
      <td><span class="style5">CARD_TYPE: </span></td>
      <td><span class="style6">
        <select name="CARD_TYPE">
          <option class="red" >Please Choose</option>
          <option class="pink" >VISA</option>
          <option class="pink" >Mastercard</option>
          <option class="pink" >American Express</option>
          </select>
      </span></td>
    </tr>
    <tr>
      <td><span class="style5">MOVIES: </span></td>
      <td><span class="style6">
        <select name="MOVIE">
          <option class="red" >Please Choose</option>
          <option class="pink" >Burn After Reading</option>
          <option class="pink" >Planet Terror</option>
          <option class="pink" >Tropic Thunder</option>
          <option class="pink" >Quantum of solace</option>
          </select>
      </span></td>
    </tr>
    <tr>
      <td><span class="style5">DATE: </span></td>
      <td><span class="style6">
        <select name="select3" id='date'>
          <option class="red" >Please Choose</option>
          <option class="pink" >October 20</option>
          <option class="pink" >October 21</option>
          <option class="pink" >October 22</option>
          <option class="pink" >October 23</option>
          <option class="pink" >October 24</option>
          <option class="pink" >October 25</option>
          <option class="pink" >October 26</option>
          <option class="pink" >October 27</option>
          </select>
      </span></td>
    </tr>
    <tr>
      <td><span class="style5"><br />
      TIME: </span></td>
      <td><span class="style6">
        <input type="radio" value="TIME" name="TIME" />
        10:40am<br />
        <input type="radio" value="TIME" name="TIME" />
        1:00pm<br />
        <input type="radio" value="TIME" name="TIME" />
        3:50pm<br />
        <input type="radio" value="TIME" name="TIME" />
        6:10pm<br />
        <input type="radio" value="TIME" name="TIME" />
        8:40pm<br />
        <input type="radio" value="TIME" name="TIME" />
      9:10pm</span></td>
    </tr>

            <tr>
              <td colspan='2' align='center'>
                <div align="right">
                  <input class="blue" type='submit' name='Submit' value='SUBMIT'>  
                </div>
                <p align="right">
      			 <input class="blue" name="Submit2" type="reset" value="CLEAR ALL FIELDS" />
   			</p>             
		  </td>
      		</tr>

		</table>        
	</td>
</tr>
</table>
  </div>
</form>
<?PHP
}//true == $show_form
?>
</body>
<html>

sorry but u really should be using a credit card gateway like paypal to valadate money.........

 

getting users credit card numbers to pay for somethink is very tricky business,

let a gateway do it all then ur get no come back.........

 

trust me..........

Just print the values the user submitted with the form.

 

<input class="violet" type="text" name="NAME" size="30" value="<?php echo $_POST['NAME'] ?>" /> 

 

Other notes:

  • as a general rule it is better to do the PHP stuff at the beginning of the script instead of embedding functions and classes in the html
  • use double quotes around HTML attributes, i.e. type="text"
  • close input tag using />

Here is a basic form and form processing code that allows you to display errors next to the form field, to redisplay previously submitted values, and to prevent duplicate form submission. It's all just some conditional statements to control what happens -

 

<?php
// basic form and form processing code on one page
// w/duplicate submission prevention (using session)

session_start();

// determine if the form processing code has been execuited once
$processed = isset($_SESSION['processed']) ? TRUE : FALSE;

// condition the form variables and setup default values -
$submitted = isset($_POST['submit']) ? $_POST['submit'] : FALSE; // was the form submitted?
$name_field = isset($_POST['name']) ? $_POST['name'] : ""; // condition the form's name field
// add more form fields here - only actual variables are set to prevent injection attempts (most looping or array  map code unconditionally sets any variable name that was submitted)

// the form processing code
// if the form was submitted and not already processed
if($submitted && !$processed) {
$form_error = array(); // array to hold any form validation errors

// validate the form data here (set elements in $form_error to hold error messages)
	if(empty($name_field)) {
	$form_error['name_field'] = "Please fill in the name";
}

// if there were no form validation errors, use the data that was submitted
if(empty($form_error)) {
	// do something with the data here
	echo "The name you entered was: $name_field<br />";
	// indicate that the form data was processed -
	$_SESSION['processed'] = true;
}
}

// the form code
// display the form if it has not been submitted or there are form validation errors
If(!$submitted || !empty($form_error)) {
$error_message = "";
// check for and display any form validation errors
if(!empty($form_error)) {
	$error_message = "Please correct the errors shown -";
}

// display the form, with any errors and  previously submitted values

?>
<?php echo $error_message; ?>
<form method="post" action="">
<label for="name">Name: <?php echo isset($form_error['name_field']) ? $form_error['name_field'] : "Required";?></label><br />
<input type="text" name="name" id="name" value="<?php echo $name_field; ?>">
<input type="submit" name="submit">
</form>
<?php
}

// if data is resubmitted/refreshed, output a meaningful message
if($processed){
echo 'Thank you, the data has already been processed.<br />';
}
?>

here a example for u if the user didnt fill in the form...

 

try it it on one page ok.......

 

<?php

if (isset($_POST['submit'])){

$users_name=$_POST['users_name'];

$card_number=$_POST['card_number'];

if (empty($users_name)) {

	$err1="<font color='red'>Please fill in username!</font>";
}

if (empty($card_number)) {

	$err2="<font color='red>Please fill in credit card number!</font>";
}


if( ($users_name) && ($card_number)){

echo "<BR><BR> <font color='green'>Your user name is: $users_name and card number is: $card_number </font> <BR><BR>";

}

}

?>

<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">

<?php  $br="<br>"; ?>

Please enter your name <?php echo $err1; ?>

<?php echo $br; ?>

<input type="text" name="users_name">

<?php echo $br; ?>

Please enter card number <?php echo $err2; ?>

<?php echo $br; ?>

<input type="text" name="card_number">

<?php echo $br;?>

<?php echo $br;?>

<input type="submit" name="submit" value="SEND INFO!">

</form>

how do i print the value for a drop down box, radio button and checkbox???

 

i tried the code below but it only works for text boxes.

 

<input class="violet" type="text" name="NAME" size="30" value="<?php echo $_POST['NAME'] ?>" />

 

and also how do i send the user to another page with all the input fields entered when the user completes the 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.