SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    625
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    show <select> option selected

    hi all

    i have two option values "bank" and "paypal" in <select> tag.

    i have the selected value getting stored in database.

    PHP Code:
    $mode $row['mode'];

    echo 
    "<select name='mode' id='mode'>"
    echo 
    "<option "; if($mode=='bank'){echo "selected='selected'";} echo " value='bank'>bank</option>";
    echo 
    "<option "; if($mode=='paypal'){echo "selected='selected'";} echo " value='paypal'>paypal</option>";
    echo 
    "</select>"

    now in my modify form i want to show the stored value as automatically selected in <select> option.

    means in the form if the customer has choosen "paypal" at the time of filing the form then the value "paypal" should automatically get selected to show the person that he had choosen "paypal" earlier.

    dont know whats getting wrong

    vineet
    Last edited by vinpkl; Jul 23, 2009 at 07:33. Reason: code

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That code seems to work for me if I make $mode equal either 'paypal' or 'bank'. Is it not working on your end? You might want to check that $mode is outputting what you expect. You might want to make a function out of it to simplify things if you're going to do something like this a bunch of times:
    PHP Code:
    <?php

    function selected($opt1,$opt2) {
        if (
    $opt1 === $opt2) echo 'selected="selected"';
    }

    ?> 
    <select name="mode" id="mode">
        <option value="bank" <?php selected($mode,'bank');?> >bank</option>
        <option value="paypal" <?php selected($mode,'paypal');?> >paypal</option>
    </select>

  3. #3
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    625
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hi funkdaddy

    the $mode is outputting correct. but its not getting selected in options.

    vineet

    Quote Originally Posted by funkdaddy View Post
    That code seems to work for me if I make $mode equal either 'paypal' or 'bank'. Is it not working on your end? You might want to check that $mode is outputting what you expect. You might want to make a function out of it to simplify things if you're going to do something like this a bunch of times:
    PHP Code:
    <?php

    function selected($opt1,$opt2) {
        if (
    $opt1 === $opt2) echo 'selected="selected"';
    }

    ?> 
    <select name="mode" id="mode">
        <option value="bank" <?php selected($mode,'bank');?> >bank</option>
        <option value="paypal" <?php selected($mode,'paypal');?> >paypal</option>
    </select>

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There must be something else, if I run your code like this:
    PHP Code:
    <?php

    $mode 
    'paypal';

    echo 
    "<select name='mode' id='mode'>"
    echo 
    "<option "; if($mode=='bank'){echo "selected='selected'";} echo " value='bank'>bank</option>"
    echo 
    "<option "; if($mode=='paypal'){echo "selected='selected'";} echo " value='paypal'>paypal</option>"
    echo 
    "</select>"

    ?>
    by itself, it works as described. If you run just this block of code do you get the same result?

  5. #5
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    625
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hi funkdaddy

    if i run your code that has $mode ='paypal';

    then it works fine as needed. means paypal gets automaticaly selected.

    vineet


    Quote Originally Posted by funkdaddy View Post
    There must be something else, if I run your code like this:
    PHP Code:
    <?php

    $mode 
    'paypal';

    echo 
    "<select name='mode' id='mode'>"
    echo 
    "<option "; if($mode=='bank'){echo "selected='selected'";} echo " value='bank'>bank</option>"
    echo 
    "<option "; if($mode=='paypal'){echo "selected='selected'";} echo " value='paypal'>paypal</option>"
    echo 
    "</select>"

    ?>
    by itself, it works as described. If you run just this block of code do you get the same result?

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Do an echo of $row['mode'] and see what's its value.
    If funkdaddy's code works, and yours doesn't, then probably the value of $row['mode'] isn't what you think it is


Bookmarks

Posting Permissions

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