0

I have the following code to parse a form submitted in EE:

foreach ($fields as $f)
{            
    if(isset($_POST[$f]) {
        $$f = $this->EE->input->post($f);
        $$f = $this->EE->security->xss_clean($$f);
        $clean_data[$f] = $$f;
    }
}

Not all members in the $fields array may be present in the post.

Does that raise an exception if the field doesn't exist or will it go ahead and create a member in $clean_data for the missing postvar?

If so, would this be a simplification of the entire loop above?

foreach ($fields as $f)
{            
    $$f = $this->EE->input->post($f, true);
    $clean_data[$f] = $$f;
}

The $fields array is needed to post to a web service. All fields are required in the $fields array but they may not be present in the post.

0

Why not skip the extra step

$clean_data = array();
foreach ($fields as $f)
{            
    $clean_data[$f] = $this->EE->input->post($f, true);
}

input->post() will return FALSE if the field was not posted.

edit: if fields should be required and contain certain types of values, you need to look at the Form Validation

http://ellislab.com/expressionengine/user-guide/development/usage/form_validation.html

http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html

5
  • Going to use client-side validation for fields in the form. No validation required for other members in the $fields array.
    – MB34
    Nov 5 '13 at 19:37
  • I keep ending up with this result when var_dumping $clean_data: array('' => false)
    – MB34
    Nov 5 '13 at 20:04
  • then your $fields array has empty values, you sure you dont need the keys instead? e.g. foreach($fields as $fieldname => $defaultvalue)
    – GDmac
    Nov 5 '13 at 21:15
  • Actually, I think I have a different way of doing it, I'll let you know soon.
    – MB34
    Nov 5 '13 at 21:23
  • Got it working. I will post the answer below.
    – MB34
    Nov 5 '13 at 21:32
0

Here is the code that works for me:

foreach ($clean_data as $key=>$value)
{            
    if(isset($_POST[$key])) {
        $clean_data[$key] = $this->EE->input->post($key, true);            
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.