I have a php page that I'm trying to simplify and am running into some issues that I can't get through alone. My form takes user data, posts to itself, validates that fields have been filled in, and then displays form contents/posts to a mysql database.
The issue I am having is that instead of having 20 if()/elseif statements I wanted to load the variable names into an array, loop through that array, and if a variable wasn't populated in the form have it produce an error message. Unfortunately my code will display the error message regardless of whether the field has a value in it or not.
As an additional note, I can add the $ShippingCo to my form and echo it but the notice that it isn't completed still shows up.
Also, if the script enters the if statement I'd like for it to stop executing the remainder of the page after the closing I've tried exit; without success.
Here is what I have:
<?php
$ShippingCo = $_POST['ShippingCo'];
$ShipAcct = $_POST['ShipAcct'];
$ShipService = $_POST['ShipService'];
$FOB = $_POST['FOB'];
$Terms = $_POST['Terms'];
$ENote[] = '$Terms';
$ENote[] = '$FOB';
$ENote[] = '$ShippingCo';
$ENote[] = '$ShipAcct';
$ENote[] = '$ShipService';
$Emessg[] = 'Shipping Terms';
$Emessg[] = 'FOB Method';
$Emessg[] = 'Shipping Company';
$Emessg[] = 'Shipping Account';
$Emessg[] = 'Shipping Service Type';
foreach ($ENote as $a => $b) {
if(!$$ENote[$a]){ //I intentionally put the '$$' in this line otherwise none of the messages show. . . with data in the variables or not.
$error = "Error! Please Add the $Emessg[$a]!";
?>
<table width="800" align="center">
<tr>
<td align="center">
<h2>Sales Order Entry Form</h2>
</td>
</tr>
<tr>
<td align="center">
<h3>
<font color="red">
<?php
echo "$error";
?>
</font>
</h3>
</td>
</tr>
<tr>
<td align="center">
Please press back to properly complete the form</td>
</tr>
</table>
<?php
}
}
?>
Thank you in advance.