Page 1 of 1

Multiple select fields Arrays

Posted: Wed Jul 08, 2009 1:50 pm
by lilycreek
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


I have an array which pulls in all form values in as

Code: Select all

foreach ($_POST as $key => $value) {
        $value = stripslashes(strip_tags($value));
 
        if (empty($value)) {
            exit("<p>Empty fields are not allowed. Please go <a href=\"javascript&#058; history.go(-1)\">Back</a> and fill in the form properly.</p>");
 
        } 
        
//if multiple select count and join option values 
if (count($_POST[$key]) >1) {
  $_POST[$key] = join(", ", $value);
 }
       else {$_POST[$key] = stripslashes(strip_tags($value));}
   
    }
This works as long as any multiple select fields i.e.

Code: Select all

<select name="mselect[]" multiple="multiple"><option>Option 1<option>Option 2</select>
have multiple options selected, but if only one selection is made it returns an empty array for that field.

I'm very new to arrays and I cannot figure out how to get a single selected option to return in the multiple select fields. Any ideas?


pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: Multiple select fields Arrays

Posted: Wed Jul 08, 2009 4:11 pm
by Eric!
I am not sure you want to do a foreach with the entire $_POST data. Anyway a good debugging technique is to use var_dump

Code: Select all

echo '<pre>'.var_dump($_POST).'</pre>;
This will tell you what all your data looks like and the structure.