Hi everyone. Let me start by saying I am pretty new to php and I am looking for the optimal way to code without using OOP. I have the following code that I wrote and I would like to know if there is a better way to do this. The logical side of my brain says that this is not really right.
What I am trying to do is error checking. I have 13 fields that need to be validated and as you can see, I have 13 POST vars. The end of the script checks that all the fields are not blank and then will insert into the db.
Can someone please offer a hand? I would be very grateful.
[PHP] if(isset($_POST['inc'])){
if($_POST['inc'] == ""){
$_POST['err1'] = "err1";
}
}
if(isset($_POST['source'])){
if($_POST['source'] == ""){
$_POST['err2'] = "err2";
}
}
if(isset($_POST['occurred_Date'])){
if($_POST['occurred_Date'] == ""){
$_POST['err3'] = "err3";
}
}
if(isset($_POST['occurred_Time'])){
if($_POST['occurred_Time'] == ""){
$_POST['err4'] = "err4";
}
}
if(isset($_POST['ap'])){
if($_POST['ap'] == ""){
$_POST['err5'] = "err5";
}
}
if(isset($_POST['first_Name'])){
if($_POST['first_Name'] == ""){
$_POST['err6'] = "err6";
}
}
if(isset($_POST['last_Name'])){
if($_POST['last_Name'] == ""){
$_POST['err7'] = "err7";
}
}
if(isset($_POST['address1'])){
if($_POST['address1'] == ""){
$_POST['err8'] = "err8";
}
}
if(isset($_POST['address2'])){
if($_POST['address2'] == ""){
$_POST['err9'] = "err9";
}
}
if(isset($_POST['zip_Code'])){
if($_POST['zip_Code'] == ""){
$_POST['err10'] = "err10";
}
}
if(isset($_POST['phone_Type'])){
if($_POST['phone_Type'] == ""){
$_POST['err11'] = "err11";
}
}
if(isset($_POST['area_Code'])){
if($_POST['area_Code'] == ""){
$_POST['err12'] = "err12";
}
}
if(isset($_POST['phone_Number'])){
if($_POST['phone_Number'] == ""){
$_POST['err13'] = "err13";
}
}
if(isset($_POST['inc']) && (isset($_POST['source']) && (isset($_POST['occurred_Date']) && (isset($_POST['occurred_Time']) && (isset($_POST['ap']) && (isset($_POST['first_Name']) && (isset($_POST['last_Name']) && (isset($_POST['address1']) && (isset($_POST['address2']) && (isset($_POST['zip_Code']) && (isset($_POST['phone_Type']) && (isset($_POST['area_Code']) && (isset($_POST['phone_Number'])))))))))))))){
if($_POST['inc'] !==""){
if($_POST['source'] !==""){
if($_POST['occurred_Date'] !==""){
if($_POST['occurred_Time'] !==""){
if($_POST['ap'] !==""){
if($_POST['first_Name'] !==""){
if($_POST['last_Name'] !==""){
if($_POST['address1'] !==""){
if($_POST['address2'] !==""){
if($_POST['zip_Code'] !==""){
if($_POST['phone_Type'] !==""){
if($_POST['area_Code'] !==""){
if($_POST['phone_Number'] !==""){
echo "INSERT";
}
}
}
}
}
}
}
}
}
}
}
}
}
} [/PHP]
What I am trying to do is error checking. I have 13 fields that need to be validated and as you can see, I have 13 POST vars. The end of the script checks that all the fields are not blank and then will insert into the db.
Can someone please offer a hand? I would be very grateful.
[PHP] if(isset($_POST['inc'])){
if($_POST['inc'] == ""){
$_POST['err1'] = "err1";
}
}
if(isset($_POST['source'])){
if($_POST['source'] == ""){
$_POST['err2'] = "err2";
}
}
if(isset($_POST['occurred_Date'])){
if($_POST['occurred_Date'] == ""){
$_POST['err3'] = "err3";
}
}
if(isset($_POST['occurred_Time'])){
if($_POST['occurred_Time'] == ""){
$_POST['err4'] = "err4";
}
}
if(isset($_POST['ap'])){
if($_POST['ap'] == ""){
$_POST['err5'] = "err5";
}
}
if(isset($_POST['first_Name'])){
if($_POST['first_Name'] == ""){
$_POST['err6'] = "err6";
}
}
if(isset($_POST['last_Name'])){
if($_POST['last_Name'] == ""){
$_POST['err7'] = "err7";
}
}
if(isset($_POST['address1'])){
if($_POST['address1'] == ""){
$_POST['err8'] = "err8";
}
}
if(isset($_POST['address2'])){
if($_POST['address2'] == ""){
$_POST['err9'] = "err9";
}
}
if(isset($_POST['zip_Code'])){
if($_POST['zip_Code'] == ""){
$_POST['err10'] = "err10";
}
}
if(isset($_POST['phone_Type'])){
if($_POST['phone_Type'] == ""){
$_POST['err11'] = "err11";
}
}
if(isset($_POST['area_Code'])){
if($_POST['area_Code'] == ""){
$_POST['err12'] = "err12";
}
}
if(isset($_POST['phone_Number'])){
if($_POST['phone_Number'] == ""){
$_POST['err13'] = "err13";
}
}
if(isset($_POST['inc']) && (isset($_POST['source']) && (isset($_POST['occurred_Date']) && (isset($_POST['occurred_Time']) && (isset($_POST['ap']) && (isset($_POST['first_Name']) && (isset($_POST['last_Name']) && (isset($_POST['address1']) && (isset($_POST['address2']) && (isset($_POST['zip_Code']) && (isset($_POST['phone_Type']) && (isset($_POST['area_Code']) && (isset($_POST['phone_Number'])))))))))))))){
if($_POST['inc'] !==""){
if($_POST['source'] !==""){
if($_POST['occurred_Date'] !==""){
if($_POST['occurred_Time'] !==""){
if($_POST['ap'] !==""){
if($_POST['first_Name'] !==""){
if($_POST['last_Name'] !==""){
if($_POST['address1'] !==""){
if($_POST['address2'] !==""){
if($_POST['zip_Code'] !==""){
if($_POST['phone_Type'] !==""){
if($_POST['area_Code'] !==""){
if($_POST['phone_Number'] !==""){
echo "INSERT";
}
}
}
}
}
}
}
}
}
}
}
}
}
} [/PHP]
Comment