Jump to content

Please I need help to understand this code


chris17

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.