Jump to content

Form Validation


suttercain

Recommended Posts

Hi everyone,

 

I wrote the following form so that if a user leaves a field blank the page will not process and instead be re-loaded with a red error message asking the user to enter the missing information:

 

<?php
  $errors=array();
  if(isset($_REQUEST['submit'])){ // If the form was submitted
     validate_input(); // Check for empty fields
     if(count($errors) != 0){ // If there are errors,
      // redisplay the form
         display_form();
     }
     else{ echo "<b>OK! Go ahead and Process the form!</b> <br />"; }
   }
   else{display_form();} // Display the form for the first time

function validate_input(){
	global $errors;
	if ($_POST["first_name"] == "") {
	$errors['first_name']="<font color='red'>
	Please enter your First Name.</font>";
	}
	if ($_POST["last_name"] == "") {
	$errors['last_name']="<font color='red'>
	Please enter your Last Name.</font>";
	}
	if ($_POST["street"] == "") {
	$errors['street']="<font color='red'>
	Please enter your Street Address.</font>";
	}
	if ($_POST["city"] == "") {
	$errors['city']="<font color='red'>
	Please enter your City.</font>";
	}
	if ($_POST["state"] == "") {
	$errors['state']="<font color='red'>
	Please select your State.</font>";
	}
	if ($_POST["zip_code"] == "") {
	$errors['zip_code']="<font color='red'>
	Please enter your Zip Code.</font>";
	}
	if ($_POST["vehicle_year"] == "") {
	$errors['vehicle_year']="<font color='red'>
	Please enter the Year of the Vehicle.</font>";
	}
	if ($_POST["vehicle_make"] == "") {
	$errors['vehicle_make']="<font color='red'>
	Please enter the Make of the Vehicle.</font>";
	}
	if ($_POST["vehicle_model"] == "") {
	$errors['vehicle_model']="<font color='red'>
	Please enter the Model of the Vehicle.</font>";
	}
	if ($_POST["vin"] == "") {
	$errors['vin']="<font color='red'>
	Please enter the Vehicle Identification Number.</font>";
	}
}

function display_form () {
	global $errors;
	?>
	<form method = "post" action = "<?php echo $_SERVER['PHP_SELF']; ?>" >
	First Name: <br />
	<input type="text" name="first_name" value="<?php echo $_POST[first_name]; ?>" /><br />
	<?php echo $errors['first_name']; ?><br />
	Last Name: <br />
	<input type="text" name="last_name" value="<?php echo $_POST[last_name]; ?>" /><br />
	<?php echo $errors['last_name']; ?><br />
	Address: <br />
	<input type="text" name="street" value="<?php echo $_POST[street]; ?>" /><br />
	<?php echo $errors['street']; ?><br />
	City: <br />
	<input type="text" name="city" value="<?php echo $_POST[city]; ?>" /><br />
	<?php echo $errors['city']; ?><br />
	State: <br />
	<input type="text" name="state" value="<?php echo $_POST[state]; ?>" /><br />
	<?php echo $errors['state']; ?><br />
	Zip Code: <br />
	<input type="text" name="zip_code" value="<?php echo $_POST[zip_code]; ?>" /><br />
	<?php echo $errors['zip_code']; ?><br />
	Vehicle Year: <br />
	<input type="text" name="vehicle_year" value="<?php echo $_POST[vehicle_year]; ?>" /><br />
	<?php echo $errors['vehicle_year']; ?><br />
	Vehicle Make: <br />
	<input type="text" name="vehicle_make" value="<?php echo $_POST[vehicle_make]; ?>" /><br />
	<?php echo $errors['vehicle_make']; ?><br />
	Vehicle Model: <br />
	<input type="text" name="vehicle_model" value="<?php echo $_POST[vehicle_model]; ?>" /><br />
	<?php echo $errors['vehicle_model']; ?><br />
	Vehicle Identification Number: <br />
	<input type="text" name="vin" value="<?php echo $_POST[vin]; ?>" /><br />
	<?php echo $errors['vin']; ?><br />
	<input type="reset" />
	<input type="submit" />
	</form>
	<?php
	}
	?>

 

The form shows up, but if I enter information it reloads the page without errors or if I fill in all the information it does not go to the "OK! Go ahead and Process the form!" message it should.

 

Where am I going wrong? This shorter version works....

 

<html><head><title>Empty Fields</title></head>
<body><div align="center">
<h2>Validating Input</h2>
<?php

  $errors=array();
  if(isset($_REQUEST['submit'])){ // If the form was submitted
     validate_input(); // Check for empty fields
     if(count($errors) != 0){ // If there are errors,
      // redisplay the form
         display_form();
     }
     else{ echo "<b>OK! Go ahead and Process the form!</b> <br />"; }
   }
   else{display_form();} // Display the form for the first time

   function validate_input(){
     global $errors;
     if($_POST["name"] == ""){
         $errors['name']="<font color='red'>
          ***Your name?***</font>";
     }
     if($_POST["phone"] == ""){
         $errors['phone']="<font color='red'>
         ***Your phone?***</font>";
     }
   }
   function display_form(){
   global $errors;

?>
  <b>
  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
   What is your name? <br />
  <input type="text" name="name"
         value="<?php echo $_POST[name]; ?>"> <br />
    <?php echo $errors['name']; ?> <br />
    What is your phone number?<br />
    <input type="text" name="phone"
           value="<?php echo $_POST[phone]; ?>">
  <br />
  <?php echo $errors['phone']; ?>
  <br />
  <input type="reset">
  <input type="submit" name="submit">
<br />
</form>
<?php
}
?>
</b>
</div>
</body>
</html>

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/45444-form-validation/
Share on other sites

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.