www.webdeveloper.com
Results 1 to 4 of 4

Thread: Setting Default Value of Select Box

  1. #1
    Join Date
    Aug 2006
    Posts
    10

    Wink Setting Default Value of Select Box

    Hey guys, I have a quick question. I have a select box in the form of:

    Code:
    <select name="country">
            <option>---</option>
    	<option selected value="US">United States</option>
    	<option value="AF">Afghanistan</option>
    	<option value="AL">Albania</option>
    	<option value="DZ">Algeria</option>
    	<option value="AS">American Samoa</option>
                                       .
                                       .
                                       .
    	<option value="EH">Western Sahara</option>
    	<option value="YE">Yemen</option>
    	<option value="ZM">Zambia</option>
    	<option value="ZW">Zimbabwe</option>
    </select>
    If the user submits the form and there is an invalid value or one of the fields are empty, I want to $_POST the value on submit and set the selected value of the select box to what the user had submitted. The $_POST returns the value that was submitted, and so then in turn, I want to set the select box to that value. I have tried several approaches with no luck. I'm sure this is something uber easy that I am just over analyzing. lol. So, any help would be appreciated. Thanks guys.

  2. #2
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,755
    This doesn't really look like a job for JavaScript. PHP is a better solution (assuming you're using PHP or Perl because you mention $_POST).
    PHP Code:
    <select name="country">
      <option>---</option>
      <option<?php if ($_POST['country'] == 'US' || empty($_POST['country'])):?> selected<?php endif;?> value="US">United States</option>
      <option<?php if ($_POST['country'] == 'AJ'):?> selected<?php endif;?> value="AF">Afghanistan</option>
      <option<?php if ($_POST['country'] == 'AL'):?> selected<?php endif;?> value="AL">Albania</option>
      <option<?php if ($_POST['country'] == 'DZ'):?> selected<?php endif;?> value="DZ">Algeria</option>
      <option<?php if ($_POST['country'] == 'AS'):?> selected<?php endif;?> value="AS">American Samoa</option>
      .
      .
      .
      <option<?php if ($_POST['country'] == 'EH'):?> selected<?php endif;?> value="EH">Western Sahara</option>
      <option<?php if ($_POST['country'] == 'YE'):?> selected<?php endif;?> value="YE">Yemen</option>
      <option<?php if ($_POST['country'] == 'ZM'):?> selected<?php endif;?> value="ZM">Zambia</option>
      <option<?php if ($_POST['country'] == 'ZW'):?> selected<?php endif;?> value="ZW">Zimbabwe</option>
    </select>

  3. #3
    Join Date
    Aug 2006
    Posts
    10
    Do you think that this would be the most efficient way to do this, considering that there are over 250 select options? I was thinking about doing it like that, but i wasn't for sure if that was the most efficient way. Thank you for the suggestion, I may end up doing that.

  4. #4
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,755
    PHP is definitely the way to go. Server side code generally runs much faster than client side code for two reasons:

    1. JavaScript interpreters in browsers are generally more processor intensive than the PHP interpreter (though I'm not 100% sure).

    2. Servers tend to have more processor and memory capacity than users' computers.

    Try this function out:
    PHP Code:
    /**
     * @function  create_select
     *    Creates an HTML SELECT tag.
     *
     * @param     $name (string, required)
     *    The name of the SELECT tag. This becomes the name attribute. The Id of
     *    the SELECT tag is the sel_$name.
     *    
     * @param     $vals (array, required)
     *    An associative array of the OPTION tag values as keys, and the OPTION
     *    tag labels as values.
     *    
     * @param     $selected (string/number, optional)
     *    The value that should be selected by default.
     *    
     * @param     $attrs (array, optional)
     *    An associative array of HTML tag attributes and values. They keys are
     *    the attribute names and the values are the attributes values.
     *    
     * @param     $isXML (boolean, optional)
     *    When true, it outputs valid XHTML, otherwise it outputs valid HTML 4.01.
     */
    function create_select($name$vals$selected=null$attrs=null$isXML=false) {
      
    $selected_attribute = !empty($isXML) ? ' selected="selected"' ' selected';
      
    $select_code "<select name='$name' id='sel_$name'";
      
      if (!empty(
    $attrs)) {
        foreach (
    $attrs as $attribute => $value) {
          if (
    strtolower($attribute) != 'multiple') {
            
    $select_code .= $attribute='$value'";
          } else {
            
    $select_code .= empty($isXML) ? ' multiple' ' multiple="multiple"';
          }
        }
      }
      
      foreach (
    $vals as $value => $label) {
        
    $select_code .= "<option value='$value'".
            (
    $value == $selected $selected_attribute '').
            
    ">$label</option>";
      }
      
      
    $select_code .= '</select>';
      
      return 
    $select_code;

    You can call it like this:
    PHP Code:
    <label for="sel_countryy">Country:</label>
    <?=create_select('country'$country_info$selected_country,
        array(
    'title'=>'Select a country'), true)?>
    The above example would output valid XHTML. You can omit the last function argument and it will output valid HTML 4.01.

    I also just realized you probably already have a function like this. You seem to have dabbled in PHP a bit. And the function is also untested.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles