SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Javascript/PHP: reselecting after $_POST

    Hello!

    I'm using the following PHP/Javascript code to create a drop down list. It's part of a longer group of code which dynamically changes a second drop down list based on what's chosen here. After the user submits their choice, I use PHP to grab relevant information, based on their choice, from my database. They're going to be going through the process several times and it would be great if I could reselect whatever they just posted. Might there be a way to reselect whatever they $_POSTed the first time? If I could have some guidance for this piece of the code, I can hopefully figure out how to implement it in my 2nd drop down menu as well.

    Thanks so much!

    -Eric

    PHP Code:
    $q1=mysql_query("select  * from assignments_topics");
    echo 
    mysql_error();
    $already_listed_chapters=array();
    while(
    $nt1=mysql_fetch_array($q1)){
    if (!
    in_array($nt1['menu_chapter_name'],$already_listed_chapters)){
        
    array_push($already_listed_chapters,$nt1['menu_chapter_name']);
        echo 
    "addOption(document.drop_list.Chapter, '$nt1[chapter_name]', '$nt1[menu_chapter_name]');";};
    }
    // end of while 

  2. #2
    SitePoint Addict skunkbad's Avatar
    Join Date
    Apr 2008
    Location
    Temecula, CA
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code PHP:
    <?php
    if( isset( $_POST['whatever'] ) )
    {
         // do something
     
         // do something with $_POST['whatever']
         echo $_POST['whatever'];
    }

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the response but I'm aware of the isset function on the php side of things. I guess that I was hoping for some help with the javascript portion. Somewhere along the line:

    echo "addOption(document.drop_list.Chapter, '$nt1[chapter_name]', '$nt1[menu_chapter_name]')

    I need to not only add that particular option, but I need to select it if it was the $_POST option; I hope that my question makes more sense now.

    -Eric

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by kreut View Post
    I need to not only add that particular option, but I need to select it if it was the $_POST option;
    You can, but it depends on how the addOption function works. Can you provide more details on that particular part?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Sure!

    I've got a drop down list named "Chapter"; each element in the list has a value "chapter_name", while the printed value is "menu_chapter_name". As my while code loops through, it goes to my database to see if the menu_chapter_name has been listed already; if not, then it prints it, but if it has, it won't print it again.

    Does that make sense?

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by kreut View Post
    Sure!

    I've got a drop down list named "Chapter"; each element in the list has a value "chapter_name", while the printed value is "menu_chapter_name". As my while code loops through, it goes to my database to see if the menu_chapter_name has been listed already; if not, then it prints it, but if it has, it won't print it again.

    Does that make sense?
    It makes sense, but it doesn't help.

    To achieve what you require takes two main steps.

    1. get from $_POST the option that was submitted
    2. tell the added option that it's the currently selected one if it matches the posted option that was submitted.

    The addOption function is what you yourself use to add an option, but that function is not a normal part of javascript.

    You would need to add a boolean parameter to the addOption function that specified whether the option is selected or not, and that can only be done by knowing the code for the addOption function.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    My apologies Paul!

    function addOption(selectbox, value, text )
    {
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;

    selectbox.options.add(optn);
    }

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Add another statement to that addOption function to set the selected property:

    Code javascript:
    function addOption(selectbox, value, text, selected) {
        var optn = document.createElement("OPTION");
        optn.text = text;
        optn.value = value;
        optn.selected = selected;
     
        selectbox.options.add(optn);
    }

    Then in the PHP, you can work out whether it should be selected or not.

    Code php:
    $selected = 'false';
    if (...) {
        $selected = 'true';
    }
    echo "addOption(..., '...', '...', $selected);";

    Now all you have to do is to work out how to tell if the option is the same as the posted one.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Clear as a bell. Thank you and enjoy your weekend.

    -Eric


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
  •