CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2010
    Posts
    11

    Unhappy Wanto to keep drop down value selected after submit

    I have a webshop. I have a little problem in the admin side.

    There's a text field what is written in the sql database after pressing submit. Also there's drop down menu where I have to select product category where I information should be send. At this moment drop down menu has 1156 categories.

    Let's say I want to export information to the category 890. I search category 890 in the list and press submit. Now page will be refreshed and now drop down menu doesn't remember what I selected before. Again I have to open drop down menu and search next category 891 and submit staff there. Now page will be refreshed and now drop down menu has again default value which is "Select". And again again again I have to scroll down to find 892.

    I want that list remembers it's value after submit is pressed.

    You know what I mean?

    List of the menu is generated like this:

    PHP Code:
    <?php

    $result 
    mysql_query ("SELECT * FROM $clover_shop_categories WHERE cs_category_title IS NOT NULL ORDER BY cs_category_listing_position asc");

    if (
    mysql_num_rows ($result)) {

        echo 
    "<SELECT name=\"import_category\">\n";

        echo 
    "<OPTION value=\"\">- choose category -</OPTION>\n";
        echo 
    "<OPTION value=\"\"></OPTION>\n";

        while (
    $s mysql_fetch_array ($result)) {

            echo 
    "<OPTION value=\"$s[cs_category_id]\">";
            for (
    $i 2$i <= $s['cs_category_level']; $i++) echo "&nbsp;&nbsp;";
            echo 
    "$s[cs_category_title]</OPTION>\n";

        }

        echo 
    "</SELECT>\n";
        
    }
    ?>
    That code is not written by me. It's a direct copy of the webshop of the importing tool.

    What I should do? Remember also that I don't know much about php...

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: Wanto to keep drop down value selected after submit

    Compare the <option> tag value with the submitted value. If they match, echo [i]selected="selected[/b] in the tag.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Jun 2010
    Posts
    11

    Re: Wanto to keep drop down value selected after submit

    Ok.... but what I should write?

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: Wanto to keep drop down value selected after submit

    PHP Code:
    $selected = ($s['cs_category_id'] == $_POST['import_category']) ? "selected=\"selected\"" "";
    echo 
    "<OPTION value=\"$s['cs_category_id']\"$selected>"
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Jun 2010
    Posts
    11

    Re: Wanto to keep drop down value selected after submit

    PHP Code:
    <?php

    $result 
    mysql_query ("SELECT * FROM $clover_shop_categories WHERE cs_category_title IS NOT NULL ORDER BY cs_category_listing_position asc");

    if (
    mysql_num_rows ($result)) {

        echo 
    "<SELECT name=\"import_category\">\n";

        echo 
    "<OPTION value=\"\">- choose category -</OPTION>\n";
        echo 
    "<OPTION value=\"\"></OPTION>\n";


        
    $selected = ($s['cs_category_id'] == $_POST['import_category']) ? "selected=\"selected\"" "";
        echo 
    "<OPTION value=\"$s['cs_category_id']\"$selected>"


        while (
    $s mysql_fetch_array ($result)) {

            echo 
    "<OPTION value=\"$s[cs_category_id]\">";
            for (
    $i 2$i <= $s['cs_category_level']; $i++) echo "&nbsp;&nbsp;";
            echo 
    "$s[cs_category_title]</OPTION>\n";

        }

        echo 
    "</SELECT>\n";
        
    }
    ?>
    Like that? I suppose no because blank screen is result.
    Last edited by Temporary-Failure; June 13th, 2010 at 03:03 PM.

  6. #6
    Join Date
    May 2002
    Posts
    10,943

    Re: Wanto to keep drop down value selected after submit

    No. The two line I supplied should replace the one two lines down. Notice the echo option similarities?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  7. #7
    Join Date
    Jun 2010
    Posts
    11

    Re: Wanto to keep drop down value selected after submit

    PHP Code:
    <?php

    $result 
    mysql_query ("SELECT * FROM $clover_shop_categories WHERE cs_category_title IS NOT NULL ORDER BY cs_category_listing_position asc");

    if (
    mysql_num_rows ($result)) {

        echo 
    "<SELECT name=\"import_category\">\n";

        echo 
    "<OPTION value=\"\">- choose category -</OPTION>\n";
        echo 
    "<OPTION value=\"\"></OPTION>\n";

        while (
    $s mysql_fetch_array ($result)) {

            
    $selected = ($s['cs_category_id'] == $_POST['import_category']) ? "selected=\"selected\"" "";
            echo 
    "<OPTION value=\"$s['cs_category_id']\"$selected>";
            for (
    $i 2$i <= $s['cs_category_level']; $i++) echo "&nbsp;&nbsp;";
            echo 
    "$s[cs_category_title]</OPTION>\n";

        }

        echo 
    "</SELECT>\n";
        
    }
    ?>
    Like this?
    Result is a blank screen.
    Last edited by Temporary-Failure; June 14th, 2010 at 01:13 AM.

  8. #8
    Join Date
    May 2002
    Posts
    10,943

    Re: Wanto to keep drop down value selected after submit

    Two possibilities.
    1. The original code doesn't have the single quotes around cs_category_I'd. Try removing them from both lines.
    2. Since you didn't post the form, I just guessed that it used POST and not GET.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  9. #9
    Join Date
    Jun 2010
    Posts
    11

    Re: Wanto to keep drop down value selected after submit

    Quote Originally Posted by PeejAvery View Post
    Two possibilities.
    1. The original code doesn't have the single quotes around cs_category_I'd. Try removing them from both lines.
    2. Since you didn't post the form, I just guessed that it used POST and not GET.
    YOUR'E THE MAN!!!

    I removed single quotes of
    echo "<OPTION value=\"$s['cs_category_id']\"$selected>";
    and it worked perfectly!!! Thank you so very much. If you some day come here in Finland I will offer you a beer.

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

Featured