Page 1 of 1

Bizarre dynamically generated checkboxes behaviour

Posted: Tue Jul 20, 2010 3:58 am
by steel_rose
Hi all,

I'm creating dynamically a series of checkboxes in a form:

Code: Select all

<?php
 //create checkboxes
                        foreach ($arrDays as $daylist => $day){

                    ?>
                            <td><input type="checkbox" name="availability[]" value ="
                            <?php

                            echo "$day"._."$slot";

                            ?>
                            "

                            
                            <?php

                            //box already checked if already in database
                            
                            if ($day._.$slot == 1){
                                
                                echo 'checked="yes"';
                                
                            }

                            ?>
                            ></td>
                            <?php
                            
                        }

                        ?>
The form displays fine, but when I collect the data there are a lot of spaces added to the value... Every checkbox should return a 7 characters string that indicates a day and a timeslot (e.g. mon_mor for monday morning) but insted I receive a string that is 64 characters long and has a lot of spaces on both sides!
This is what I get with a var_dump:

[text]
array
'availability' =>
array
0 => string '
mon_mor ' (length=64)
1 => string '
thu_mor ' (length=64)
2 => string '
wed_aft ' (length=64)


[/text]

Of course I could trim the results but I'm sure there should be a more elegant way to do this... and I'd love to understand what's happening...


Thank you very much for any help,

SR

Re: Bizarre dynamically generated checkboxes behaviour

Posted: Tue Jul 20, 2010 8:06 am
by lettie_dude
This bugged me for ages. If your using dreamweaver it would appear to be a bug in the whitespace coding. You need to make sure you have no space between your php tags and the value field ie:

Code: Select all

value ="<?php

                            echo "$day"._."$slot";

                            ?>"
Make sure the quotes rest up against the php tags. "<?php and ?>"

This should sort you out.

Re: Bizarre dynamically generated checkboxes behaviour

Posted: Tue Jul 20, 2010 3:54 pm
by steel_rose
Thank you so much!

Problem solved!

SR