sleepyw Posted March 22, 2010 Share Posted March 22, 2010 I recently got help here on an issue regarding multiple selections from a form drop-down box. Everything seemed to work fine at first, but now I'm seeing a hiccup and don't understand what I'm doing wrong. I have a multiple select HTML dropdown box with values that includes multiple words. Let's say, for example, the options in that box are Red, Blue, Light Blue, Dark Blue. When I submit the form, it implodes the values with this: $color = implode(", ", $_POST['color']); to string them together like this in the db "Red, Blue, Light Blue, Dark Blue". So far, so good. Now, when a user goes in to edit that form again, I explode the array so that the colors previously selected will show up as separate items in the dropdown box, like this: $color_exploded = explode(", ", $rows['color']); if(is_array($color_exploded)) { while (list ($key, $color_val) = each ($color_exploded)) { echo "<option value=" . $color_val . " selected>" . $color . "</option>"; } } So far, so good. Now here's where the problem occurs. If one of the colors selected has 2 words with a space between, like Light Blue, and the user resubmits the form without changing that value, the PHP cuts off everything after the first word and just displays "Light" as the value. Where is my code screwed up or what do I need to add to make sure it keeps the entire value after the space? Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted March 22, 2010 Share Posted March 22, 2010 convert the spaces to underscores for the value="". This should resolve it (and remove them when you want to display the text). If you don't know how, use str_replace() Quote Link to comment Share on other sites More sharing options...
sleepyw Posted March 22, 2010 Author Share Posted March 22, 2010 I can see how that would work, but it's not preferable in this case. If there's no other workaround, I'll have to do that, but the problem is the db fields also get printed to a report that exports to Excel, and it might be weird or confusing to some people to see values like "Light_Blue" instead of "Light Blue," even though I can "fix" the way that shows on the web page. Essentially, I have 2 different values now (the way I make it look on the web page without the underscore, then the real value as stored in the db). Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted March 22, 2010 Share Posted March 22, 2010 Well in that case you can just change it to echo "<option value=" . str_replace(' ', '_', $color_val) . " selected>" . $color . "</option>"; And then just remove the underscores before inserting into the database Quote Link to comment Share on other sites More sharing options...
sasa Posted March 22, 2010 Share Posted March 22, 2010 try $color_exploded = explode(", ", $rows['color']); if(is_array($color_exploded)) { while (list ($key, $color_val) = each ($color_exploded)) { echo "<option value='" . $color_val . "' selected>" . $color . "</option>"; //add some ' } } Quote Link to comment Share on other sites More sharing options...
ignace Posted March 22, 2010 Share Posted March 22, 2010 if(is_array($color_exploded)) { is obsolete as explode only returns something different then an array when: If delimiter is an empty string (""), explode() will return FALSE. echo "<option value='" . $color_val . "' selected>" . $color . "</option>"; //add some ' I would replace that with: echo '<option value="',$color_val,'"',(($color_val === $chosen_color)?' selected="selected"':''),'>',$color,'</option>'; Quote Link to comment Share on other sites More sharing options...
Bio Posted March 22, 2010 Share Posted March 22, 2010 How about exploding only using the coma leaving out the space? $color_exploded = explode(",", $rows['color']); then use SUBSTR to remove any leading spaces prior to display. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.