It might be easiest to have a configuration, that tells PHP how the textbox should be formatted (min/max length, numbers only, alpha only, etc.).
It would/could look something like this:
$config = [
"myField" => [
"max" => 15,
"min" => 6
]
]; // Array is php 5.4 syntax
function validate_field($field_name, $value, &$error){
global $config; // don't use global, I am for this example though
$settings = $config[$field_name];
if(isset($settings["max"]) && $settings["max"] > strlen($value)){
$error = "Too Long";
return false;
}
if(isset($settings["min"]) && $settings["min"] < strlen($value)){
$error = "Too Short";
return false;
}
$error = "";
return true;
}
$errors = [];
foreach($_POST as $name => $value){
if(!validate_field($name, $value, $error)){
// The field wasn't valid
$errors[] = $error;
}
}
if(count($errors) > 0){
// The form has errors
echo implode(",", $errors);
// Show form again
exit;
}
header("Location: to success");
exit;