chris17 Posted November 11, 2013 Share Posted November 11, 2013 I have this code in my application for form validation. it is working fine but i just copied and applied it in my work, i don't really understand how it works. This is it: $required_fields = array('menu_name', 'position', 'visible');foreach($required_fields as $fieldname) { if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname]) && ($_POST[$fieldname] != 0)){ $error[] = $fieldname; } }if(!empty($error)){ redirect_to("new_subject.php"); } //Field Length$fields_with_lengths = array('menu_name' => 30);foreach($fields_with_lengths as $fieldname => $maxlength){ if(strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength){ $error[] = $fieldname; } } Link to comment https://forums.phpfreaks.com/topic/283810-please-i-need-help-to-understand-this-code/ Share on other sites More sharing options...
Ch0cu3r Posted November 11, 2013 Share Posted November 11, 2013 It loops through the list of field names in the $required_fields array and If they don't exist in the $_POST array or their values is empty it's add the field to the $error array It then validates the field value lengths. It uses the field name as the key and the maximum string length as the value in the $fields_with_length array. It then loops though that array and sets the field to $error if the field length exceeds the string length assigned to it. For example if the field named menu_name has a string length of 30 or more then that field will be added to the $error array. Link to comment https://forums.phpfreaks.com/topic/283810-please-i-need-help-to-understand-this-code/#findComment-1457888 Share on other sites More sharing options...
dalecosp Posted November 11, 2013 Share Posted November 11, 2013 Looks like it's just some sort of POST field validator: //create an array $required_fields = array('menu_name', 'position', 'visible'); //loop through it; it appears we'll be checking to see what was POSTed from a web form. foreach($required_fields as $fieldname) { //if the field is not set in POST, or the field is set but empty and non-zero ... I *think*. This line actually looks rather suspicious. if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname]) && ($_POST[$fieldname] != 0)){ $error[] = $fieldname; //there's an error, so we're recording which field it was in } }//end foreach loop if(!empty($error)){ // we have something in the $error array, so we'll redirect? redirect_to("new_subject.php"); // this must be a custom function, as no such call exists in PHP AFAIK. } //Now it appears we'll do the same thing, just checking one field, "menu_name" and making sure it's less than 30 characters. //Field Length $fields_with_lengths = array('menu_name' => 30); foreach($fields_with_lengths as $fieldname => $maxlength){ if(strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength){ $error[] = $fieldname; } } Link to comment https://forums.phpfreaks.com/topic/283810-please-i-need-help-to-understand-this-code/#findComment-1457889 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.