Results 1 to 8 of 8

Thread: [RESOLVED] Question about dropdown combo

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    30

    Resolved [RESOLVED] Question about dropdown combo

    Hi all I have a question regarding with dropdown combo. May I know how to view back the data at dropdown combo so that I can edit it?

    Example here:
    I have saved the data about my personal. In that form I have selected the wrong country. Then I want to view this data to edit the country. What I cannot do is I want to display the previous country that I selected before at that dropdown combo. Can someone please tell me the simple way how to do it?

    Thanks

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Question about dropdown combo

    If it's stored in a database you could do something like this: (I assume you're only having problems making the current country display as 'selected' in the option values) I also don't know how you're listing your countries, so I put some in an array. just a few though.
    PHP Code:
    <select name="country"><?php
      
    @extract(mysql_fetch_assoc(mysql_query("SELECT country as thiscountry FROM table_name WHERE username='test' LIMIT 1")));
      if(isset(
    $thiscountry)){
        
    $countries = array("Canada""United States""Vietnam""Japan""Russia""France""England""Germany""Egypt""Chad""Afghanistan""Iraq""Mexico");
        
    sort($countries)
        foreach(
    $countries as $country){
    ?>
      <option value="<?php echo $country?>"<?php if($country == $thiscountry) echo ' selected="selected"'?>><?php echo $country?></option>
    <?php
        
    }
      }
    ?>
    </select>
    This should properly highlight the user's current country when loading the page.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    30

    Re: Question about dropdown combo

    Thanks a lot Mr.Kows for always answered me..but i dont understand your coding.I pulled the country name from database..this is my codes..hope you can help me.

    PHP Code:
    <select name="country">
    <option>Select Country</option>
    <? 
        $query=mysql_query("SELECT * FROM Country ORDER BY country_name");
        while($edit=mysql_fetch_array($query))
        {?>
        <option value="<? echo $edit['country_id']?>">
        <? echo $edit['country_name']?>
        </option>
        <? 
        }    
        ?>

    </select>
    Thanks in advance

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Question about dropdown combo

    it probably isn't working simply because you have a line break after the <option> and before the </option>. It doesn't support multiple lines, so it's most likely just displaying blank. Try changing it to:
    PHP Code:
     <select name="country">
    <option>Select Country</option>
    <? 
        $query=mysql_query("SELECT * FROM Country ORDER BY country_name");
        while($edit=mysql_fetch_array($query))
        {?>
        <option value="<? echo $edit['country_id']?>"><? echo $edit['country_name']?></option>
        <? 
        }    
        ?>
    </select>

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    30

    Re: Question about dropdown combo

    Thanks a lot Mr.Kows

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    30

    Re: Question about dropdown combo

    I got the solution now..but it is not working properly..
    i can display the country data in dropdown in Edit form.
    But if the country no need to change, it will return empty data after i hit Edit Button.
    I must always select the country in dropdown even i dont want to edit that.
    Here is my codes..please let me know how to settle this problem..thanks
    PHP Code:
    <select name="country">
    <option selected value="<? echo $country ?>"><? if($country!=""){echo $country;} else {echo "";} ?></option>
    <option value="">-------------</option>
     <?
    $sqlst = mysql_query("SELECT * FROM Country ORDER BY country_name");
    while($coun = mysql_fetch_array($sqlst)){ ?>
    <option value="<? echo $coun['country_id'] ?>"><? echo $coun['country_name'] ?>
    </option>
    <? } ?>
    </select>

  7. #7
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Question about dropdown combo

    you can simply rearrange the code you already have into something that will work correctly for you.

    PHP Code:
    <select name="country">
    <option value="">-------------</option>
     <?
    $sqlst = mysql_query("SELECT * FROM Country ORDER BY country_name");
    while($coun = mysql_fetch_array($sqlst)){ ?>
    <option value="<? echo $coun['country_id'] ?>"<?php if($coun['country_name'] == $country) echo ' selected="selected"'?>><? echo $coun['country_name'] ?>
    </option>
    <? } ?>
    </select>
    I'm not sure if $country holds the name of the country or the ID (BUT, I assume it holds the name judging by the code you've provided), so you can change the statement I added to fit your needs better (to either $coun['country_id'], or keep it as $coun['country_name'])

    hope that helps.

    edit: also, I think the reason why yours kept submitting as blank was because you're defining your <option>'s value attribute as a string ($country) instead of a number (the country ID). I assume this is what is giving you the problem you had, but the code I posted above will fix this for you.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    30

    Smile Re: Question about dropdown combo

    for Mr.Kows..

    Thanks a lot..

Posting Permissions

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



Click Here to Expand Forum to Full Width