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.