Jump to content

<option selected="selected"> explanation please


helloise

Recommended Posts

i have the following code that produces a drop down list:

 

<select name="services">

          <?php

         

              $catcher_names = LpmCatcherPeer::getByAllNames();

              foreach($catcher_names as $row)

              {

                  ?>

                    <option value="<?php echo $row->getName()."/".$row->getId();?>"<?php

                      if($row->getName() === $catcher_name) echo 'selected="selected"';?>><?php echo $row->getName();?></option>

                    <?php

              }   

          ?>

</select>         

 

can someone explain the echo 'selected="selected"' part to me please...i got this code off the net and it works perfect but dont know what that part does :( :(

im trying to learn html and php please...

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/225706-explanation-please/
Share on other sites

That sets the option it is contained within as the selected option. So if you have:

 

option 1

option 2

option 3 (with selected="selected")

option 4

 

The third option will be selected on page load.

 

The reason it is written that way is because XHTML (not HTML) requires that all attributes have a value. So the attribute is 'selected' and the value of that attribute is 'selected'.

Link to comment
https://forums.phpfreaks.com/topic/225706-explanation-please/#findComment-1165486
Share on other sites

It basically sets the default option that will be selected. This is becoming more and more important for User Centered Design. There is a UCD design pattern called "Good Defaults." The pattern basically calls for us designers to consider what the best defaults are as we design a form from the user's viewpoint. For example if the drop down was asking the person to select which country they were from and you knew that 90% of your customer base lived in the United States then you would want to set United States as your default option.

Link to comment
https://forums.phpfreaks.com/topic/225706-explanation-please/#findComment-1166830
Share on other sites

Somewhat alienating to the other 10% of us though. Better to check the user's IP address for location and try to go off that. But even then you will run into some issue with countries that may be close to each other, but have bad relationships, if you get the wrong country from the IP.

Link to comment
https://forums.phpfreaks.com/topic/225706-explanation-please/#findComment-1166852
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.