vendredi 26 décembre 2014

Dynamic Required Fields in Form

I have a long form with too many fields. I wanted to make it a dynamic form, meaning that the required fields are different each time as desired. So i put a small beside all form fields and have this code to dynamically choose which fields are required and check it, therefore when the user submit the form, the dynamic error text appears only beside the selected required fields. but the outcome is different that expected. The PHP error is being displayed beside all fields not only the desired in array. Please need your help.



$required = array('cm_name', 'cm_mobile', 'cm_mail', 'cm_dob', 'cm_income', 'cm_country');

if(isset($_POST['submit'])) {

foreach($required as $field) {
if (empty($_POST[$field])) {
$err_req = "* Required Data";
$valid = true;
} else {
$err_req = "";
$valid=false; }
}

}

Aucun commentaire:

Enregistrer un commentaire