0

I have a form which contains a dropdown list of countries generated from database. The values are stored in database. There is an option in which user can view or update the valuesinserted. For updating all the form values get fetched from database. What i require is that when form is loaded for updating the selected option of the country dropdown must be that stored in the database. For eg: if from the following dropdown option2 is selected and inserted into database.

 Dropdown: |option1|<selected>
           |option2|
           |option3|

during update it should be like this

 Dropdown: |option1|
           |option2|<selected>
           |option3|

Here is the code i tried.

      $selected = $list["country_country_name"];

     <tr><td>Country</td><td><select onchange="getCountry(this.value);" name="country" id="country" ><?php  foreach( $query as $qry ) { 
     print '<option value="'.$qry["country_country_name"].'"'; 
     if( $qry["country_country_name"] == $selected ) print'selected'; 
     print '>'.$qry["country_country_name"].'</option>'."\n";} ?>
     </select></td></tr>
3
  • the person who downvoted pls tell the reason so that i can make my question clear if thats the reason Commented Oct 9, 2012 at 11:58
  • why dont you use a select ? with an list of options ? with that you can fetch the selected option. do you want an example? Commented Oct 9, 2012 at 11:59
  • i am populating my dropdown from db. if this is not what u meant pls provide eg: Commented Oct 9, 2012 at 12:01

3 Answers 3

1
<select id="list" name="list">
    <option value=""> Please Select </option>
    <?
        $list = array('1',
                        '2',
                        '3',
                        '4',
                        '5',
                        '6');

        while ($L = array_shift($list)) {
            ?>
                <option value="<?=$L?>" <? if($selected == $L){ echo 'selected="selected"'; }?> > 
                    <?= $L ?> 
                </option> 
            <?
         }
    ?>
</select>

you can simple get the selected option with this:

$("#list").val();

try please.

Sign up to request clarification or add additional context in comments.

Comments

0

$selected = $list["country_country_name"];

 <tr><td>Country</td>
 <td>
 <select onchange="getCountry(this.value);" name="country" id="country" >
 <?php  foreach( $query as $qry ) { 
    $sel = '';
   if( $qry["country_country_name"] == $selected ) 
    $sel = 'selected="selected"';           

    echo '<option value="'.$qry["country_country_name"].'" '.$sel.'>'.$qry["country_country_name"].'</option>'."\n";
    } ?>
    </select>
    <?php echo form_error('country'); ?>
    </td>
 </tr>

Comments

0

When your option tag's value attribute contains the same value as the option's text, the value attribute is not necessary -- so omit it.

Below shows an inline condition statement.

<tr>
    <td>Country</td>
    <td><select onchange="getCountry(this.value);" name="country" id="country">
        <?php
        foreach ($query as $row) { 
            echo "<option" ,
                ($row["country_country_name"] == $list['country_country_name'] ? ' selected' : '') ,
                "{$row['country_country_name']}</option>\n";
        }
        ?>
    </select></td>
</tr>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.