Bizarre dynamically generated checkboxes behaviour

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
steel_rose
Forum Newbie
Posts: 15
Joined: Thu May 13, 2010 9:17 am

Bizarre dynamically generated checkboxes behaviour

Post 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
lettie_dude
Forum Commoner
Posts: 65
Joined: Thu Dec 07, 2006 10:10 am

Re: Bizarre dynamically generated checkboxes behaviour

Post 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.
steel_rose
Forum Newbie
Posts: 15
Joined: Thu May 13, 2010 9:17 am

Re: Bizarre dynamically generated checkboxes behaviour

Post by steel_rose »

Thank you so much!

Problem solved!

SR
Post Reply