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
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
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
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
Share on other sites

The country was just an example of using Good Defaults. There are probably better ways you can get the country like you said, but the design pattern is still essential to UCD. I was just trying to explain the proper application of using "selected."  ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.