简体   繁体   中英

Validate form errors before going to action page

I need to validate errors before it goes to action url of the form. When i click submit it goes to the /test/policy.php even though there are errors. I want it to go there only if there are no errors in my form. Here is my code so far:

// Create an empty array to hold the error messages.
$arrErrors = array();
//Only validate if the Submit button was clicked.
if (!empty($_POST['submit']))
 {

      // Each time theres an error, add an error message to the error array
      // using the field name as the key.
      if (empty($_POST['first_name']))
          $arrErrors['first_name'] = '<br><font color="red">Please provide your first name.</font>';
      if (empty($_POST['last_name']))
          $arrErrors['last_name'] = '<br><font color="red">Please provide your last name.</font>';
      if (empty($_POST['business_name']))
          $arrErrors['business_name'] = '<br><font color="red">Please provide your business name.</font>';
 }


      // If the error array is empty, there were no errors.
      // Insert form processing here.

      if (count($arrErrors) == 0)
      {


        $first_name=cleaninput($_POST['first_name']);
        $last_name=cleaninput($_POST['last_name']);
        $business_name=cleaninput($_POST['business_name']);
        //Insert the details into database

      }
      else
      {

        // The error array had something in it. There was an error.
        // Start adding error text to an error string.
        $failure_message= "There was an error in the form";

        $strError="";
        foreach ($arrErrors as $error)
        {
           $strError .= $error;
        }

      }
    }
 ?>



<tr>
   <td>
   <? if(!empty($success_message))
      {
        echo "<center><font color='blue'><b>".$success_message."</b></font><br>";
      }
      if(!empty($failure_message))
      {
         echo "<center><font color='red'><b>".$failure_message."</b></font><br>";
      }
   ?>

<?
if((empty($_POST)) || !empty($strError))
{
     echo "<table align= 'center' width='70%' style='border-top: 1px; border-right: 1px; border-left: 1px; border-bottom: 1px; border-color: black; border-style: solid;'>";
?>
<tr>
  <td>

   <form action="/test/policy.php" method="post">
      <tr height='40'>
         <td>
           <font color="black"><B> First Name: </B></font>
         </td>
     <td><input type="text" size ="40" name="first_name" value="<? if(!empty($strError)) {echo cleaninput($_POST['first_name']);}?>" class="advertising-inputbox-style" />
            <?php if (!empty($arrErrors['first_name'])) echo $arrErrors['first_name']; ?>
     </td>

      </tr>
      <tr height='40'>
         <td><font color="black"><B> Last Name: </B></font></td>
         <td><input type="text" size ="40" name="last_name" value="<? if(!empty($strError)) { echo cleaninput($_POST['last_name']);}?>" class="advertising-inputbox-style"/>
         <?php if (!empty($arrErrors['last_name'])) echo $arrErrors['last_name']; ?>
             </td>
      </tr>

      <tr height='40'>
         <td><font color="black"><B> Email Address: </B></font></td>
         <td><input type="text" size ="40" name="email_address" value="<? if(!empty($strError)) { echo cleaninput($_POST['email_address']);}?>" class="advertising-inputbox-style"/>
         <?php if (!empty($arrErrors['email_address'])) echo $arrErrors['email_address']; ?>
             </td>
      </tr>
       <tr height='35'>
         <td><font color="black"><B> Business Name: </B></font></td>
         <td><input type="text" size ="40" name="business_name" value="<? if(!empty($strError)) { echo cleaninput($_POST['business_name']);}?>" class="advertising-inputbox-style" />
          <?php if (!empty($arrErrors['business_name'])) echo $arrErrors['business_name']; ?>
             </td>
      </tr>

      <tr height='35'>
         <td></td>
         <td><input type="submit" name="submit" value="submit" /></td>
      </tr>

   </form>

  <?}?>
  </td>
   </tr>

</table>

You could leave the action blank and then, after validating, you can redirect to /test/policy.php with header .

if (count($arrErrors) == 0) {  
    header('Location: /test/policy.php');  
}

If the code above is in one file, then you have to redirect the user only if there are no errors. so, let's say this file is called index.php, then the form would have the action="index.php" and goes to self after submiting:

<form action="/test/index.php" method="post">

but after you insert the users form data into database, you redirect the user to /test/policy.php, like this:

//Insert the details into database
your database insert code
....
header('Location: /test/policy.php');
die();

no, you can't use POST after redirect, but if you need the first_name, you already have it in database. so send user id in GET when redirecting, like this:

//Insert the details into database
your database insert code
$user_id = mysql_insert_id();
....

header('Location: /test/policy.php?user_id='.$user_id);
die();

in policy.php you select user field first_name from database

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM