I'm writing a form that uses some PHP validation before it is processed, here is the code i'm using to validate one "set" of fields, for ordering an item:
The only thing is, i also have 5 of these "sets" of fields in total, so currently have this:
Is there any way of simplifying it so i dont have to have the code over and over again with just a different number?!
Code:
if($_POST['item2']<>"n/a") {
$error['item2'] = false;
if (strlen($_POST['item2']) < 4) {
$error['item2'] = true;
$print_again = true;
}
$error['quantity2'] = false;
if (is_numeric($_POST['quantity2'])) {
} else {
$error['quantity2'] = true;
$print_again = true;
}
$error['reason2'] = false;
if (strlen($_POST['reason2']) < 10) {
$error['reason2'] = true;
$print_again = true;
}
$error['type2'] = false;
if ($_POST["type2"]=="") {
$error['type2'] = true;
$print_again = true;
}
if($_POST['type2']=="Replacement") {
if ($_POST["asset2"]=="") {
$error['asset2'] = true;
$print_again = true;
}
}
The only thing is, i also have 5 of these "sets" of fields in total, so currently have this:
Code:
if($_POST['item2']<>"n/a") {
$error['item2'] = false;
if (strlen($_POST['item2']) < 4) {
$error['item2'] = true;
$print_again = true;
}
$error['quantity2'] = false;
if (is_numeric($_POST['quantity2'])) {
} else {
$error['quantity2'] = true;
$print_again = true;
}
$error['reason2'] = false;
if (strlen($_POST['reason2']) < 10) {
$error['reason2'] = true;
$print_again = true;
}
$error['type2'] = false;
if ($_POST["type2"]=="") {
$error['type2'] = true;
$print_again = true;
}
if($_POST['type2']=="Replacement") {
if ($_POST["asset2"]=="") {
$error['asset2'] = true;
$print_again = true;
}
}
if($_POST['item3']<>"n/a") {
$error['item3'] = false;
if (strlen($_POST['item3']) < 4) {
$error['item3'] = true;
$print_again = true;
}
$error['quantity3'] = false;
if (is_numeric($_POST['quantity3'])) {
} else {
$error['quantity3'] = true;
$print_again = true;
}
$error['reason3'] = false;
if (strlen($_POST['reason3']) < 10) {
$error['reason3'] = true;
$print_again = true;
}
$error['type3'] = false;
if ($_POST["type3"]=="") {
$error['type3'] = true;
$print_again = true;
}
if($_POST['type3']=="Replacement") {
if ($_POST["asset3"]=="") {
$error['asset3'] = true;
$print_again = true;
}
}
// ..etc etc etc for $variable4, 5.
Is there any way of simplifying it so i dont have to have the code over and over again with just a different number?!