I would like to make certain that every field in a form is being filled out.
I've tried quite a few different ways, some work and some do not; the most tedious way (which works) is as follows:
<?php
$customer = $_POST['customer'];
$labor = $_POST['labor'];
$parts = $_POST['parts'];
$partscost = $_POST['partscost'];
$partsprofit = $_POST['partsprofit'];
$totalprofit = $_POST['totalprofit'];
$taxcollected = $_POST['taxcollected'];
if(!$customer) { echo "You did not fill in the Customer field"; die(); }
if(!$labor) { echo "You did not fill in the Labor field"; die(); }
if(!$parts) { echo "You did not fill in the Parts field"; die(); }
if(!$partscost) { echo "You did not fill in the Parts Cost field"; die(); }
if(!$partsprofit) { echo "You did not fill in the Parts Profit field"; die(); }
if(!$totalprofit) { echo "You did not fill in the Total Profit field"; die(); }
if(!$taxcollected) { echo "You did not fill in the Tax Collected field"; die(); }
?>
Yes, tedious! Long, boring and.. oy.
Is there a better way to do this with just a few lines of code?
I've tried a few ways that are a lot like this one; but they don't work ( the empty form gets input into the database anyway)
<?php
$available = array (
"Customer" => "$customer",
"Labor" => "$labor",
"Parts" => "$parts",
"Parts Cost" => "$partscost",
"Parts Profit" => "$partsprofit",
"Total Profit" => "$totalprofit",
"Tax Collected" => "$taxcollected"
);
foreach ($available as $required) {
foreach ($required as $key=>$value) {
if ($value == "" ) { echo "You did not fill in the <b>$key</b> field"; die(); }
}
}
?>
I know it's late and thats probably why the code above makes no sense. lol.
Anyway, anyone know of a sure fire way ( that isn't long and tedious ) to make certain all variables contain a string or int?