Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7436837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:18:13+00:00 2026-05-29T10:18:13+00:00

I need to validate errors before it goes to action url of the form.

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:18:14+00:00Added an answer on May 29, 2026 at 10:18 am

    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');  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to validate a form in php and display some error messages in
I have a registration form on my website and need to validate the input
I need to validate input using JavaScript and jQuery when someone submit my newsletter
I need to validate a date/time field on a webpage but want it to
I need to validate an XML string (and not a file) against a DTD
I need to validate the email address of my users. Unfortunately, making a validator
I need to validate this simple pick list: <select name=<%= key %>> <option value=ETC
I need to validate a certain property to one of my classes. When I
I need to validate the user input of a JSpinner , and if invalid,
I need to validate an XML agaist a schema. I tried XML::SAX::ParserFactory; XML::Validator::Schema and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.