SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Zealot Tikila's Avatar
    Join Date
    Jun 2007
    Location
    Toronto
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Drop down values redisplay

    I have this
    <form name="myform" action="action.php" method="post">
    <select name="myoptions">
    <option value="option1">option1</option>
    <option value="option2">option2</option>
    <option value="option3">option3</option>
    <option value="option4">option4</option>
    <option value="option5">option5</option>
    </select>
    </form>

    And i want to apply the following code on it(gratefully provided by Byron3@earthlink),
    how would i integrate this?

    function DisplayMenuChoices( $selected ="") {
    $list = array( "option1",
    "option2",
    "option3",
    "option4",
    "option5"
    );
    echo "<select name='myoptions '>\n";
    foreach ( $list as $name ) {
    if ( $name == $selected ){
    echo " <option selected='selected'>$name</option>\n";
    } else {
    echo " <option>" .$name. "</option>\n";
    }
    }
    echo "</select>\n"; // End select list
    }

    I would prefer this method,if it does not generate duplicates
    Appreciate your support

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    /* Take the function and place it in php tags <?php Function_goes_here ?> */

    <form name="myform" action="action.php" method="post">
    <? DisplayMenuChoices(); ?> // When you get ready to display this with a user selected value. Place the value in the ( $user_selected_value )
    </form>
    Does this head you in the right direction?
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  3. #3
    SitePoint Zealot Tikila's Avatar
    Join Date
    Jun 2007
    Location
    Toronto
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When i replace
    <select name="myoptions">
    <option value="option1">option1</option>
    <option value="option2">option2</option>
    <option value="option3">option3</option>
    <option value="option4">option4</option>
    <option value="option5">option5</option>
    </select>
    in the redisplay page,the menu is not ouputt

  4. #4
    SitePoint Zealot Tikila's Avatar
    Join Date
    Jun 2007
    Location
    Toronto
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Has anyone implemented this successfully before?
    I'd appreciate a working example

  5. #5
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tikila post me your redisplay code.
    Use the php block tags to enclose your code, click on the "Go Advanced" button below. The icon next to the box that has syntax in it will put a set of php block tags on this page. Just roll your mouse over it and the tool tip will read "Wrap [php] tags around selected text". Click on that icon and you will a pair of "[ ]" "[ ]" with php in the first set of brackets and /php in the second set of brackets, insert your code block between the two.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  6. #6
    SitePoint Enthusiast thenk83's Avatar
    Join Date
    Sep 2005
    Location
    Hacienda Heights, CA
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this Tikila

    Code:
    <? 
    function DisplayMenuChoices( $selected ="") {
    $list = array( "option1", "option2", "option3", "option4", "option5");
    echo "<select name='myoptions '>\n";
    foreach ( $list as $name ) {
    if ( $name == $selected ){
    echo " <option selected='selected'>$name</option>\n";
    } else {
    echo " <option>" .$name. "</option>\n";
    }
    }
    echo "</select>\n"; // End select list
    }
    
    // YOU NEED THE CODE BELOW TO DISPLAY THE DROP MENU
    DisplayMenuChoices();
    ?>

  7. #7
    SitePoint Zealot Tikila's Avatar
    Join Date
    Jun 2007
    Location
    Toronto
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you guys for your help
    As Thenk83 emphasized at the bottom of his code,i was missing the DisplayMenuChoices(); at the bottom to ouput the menu.Now that i have the menu displayed when i select,say option 3 in page 1,it does not redisplay as option3 in page2(redisplaying page).
    I must be missing something then.
    The function that i currently use works fine,except it duplicates entries which is confusing to users,i dont like it.
    Here's my current function:

    <select name="myoptions">
    <?php if(isset($_POST['myoptions'])) {
    ?>
    <option value="<?php echo ($_POST['myoptions']); ?>" selected><?php echo ($_POST['myoptions']); ?></option>
    <?php
    }
    ?>
    <option value="option1">option1</option>
    <option value="option2">option2</option>
    <option value="option3">option3</option>
    <option value="option4">option4</option>
    <option value="option5">option5</option>
    </select>

  8. #8
    SitePoint Enthusiast thenk83's Avatar
    Join Date
    Sep 2005
    Location
    Hacienda Heights, CA
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You missing name="myoptions" on each <option></option> tag.

    should be..
    <option name="myoptions" value="option3">option3</option>.

    You're calling a variable ($_POST['myoptions']) that doesn't exist.
    Mmmmm .NET...PHP...CF...
    AIM: THENK83
    MSN: thenk83@hotmail.com

  9. #9
    SitePoint Zealot Tikila's Avatar
    Join Date
    Jun 2007
    Location
    Toronto
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh,am sorry,but you're referring to my current function that i dont intend to use because of the duplicate thing.
    I want to use Byron code but am unable to get a selected option redisplay on another page.I have the code set as exactly you put above.

  10. #10
    SitePoint Enthusiast thenk83's Avatar
    Join Date
    Sep 2005
    Location
    Hacienda Heights, CA
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so then.. i assume for each option value it looks like this:

    <option name="option3" value="anotherpage?myoptions=option3">option3</option>..
    thats the only way i know it will work with calling $_POST['myoptions']..

    maybe i'm still not understanding.. sorry
    Mmmmm .NET...PHP...CF...
    AIM: THENK83
    MSN: thenk83@hotmail.com

  11. #11
    SitePoint Zealot Tikila's Avatar
    Join Date
    Jun 2007
    Location
    Toronto
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by thenk83 View Post
    so then.. i assume for each option value it looks like this:

    <option name="option3" value="anotherpage?myoptions=option3">option3</option>..
    thats the only way i know it will work with calling $_POST['myoptions']..

    maybe i'm still not understanding.. sorry
    Ok,there is no $_POST['myoptions'] in the function DisplayMenuChoices Byron suggested above,but i cannot get a selected option to redisplay as default in the following page(a confirmation page that serves as a last chance for the user to check their info before final submission).
    As for my current function that has the $_POST['myoptions'] it does work the way i coded it above redisplaying exactly what was selected previously(even with no <option name="myoptions"> that you noted,except i end up with duplicate entries in the drop down menu.

  12. #12
    SitePoint Enthusiast thenk83's Avatar
    Join Date
    Sep 2005
    Location
    Hacienda Heights, CA
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    actually no i was wrong.... you dont need name in the <option></option>. I dont know what I'm thinking. I don't see why its giving duplicate entries..
    Mmmmm .NET...PHP...CF...
    AIM: THENK83
    MSN: thenk83@hotmail.com

  13. #13
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <select name="myoptions">
    <?php if(isset($_POST['myoptions'])) {
    ?>
    <option value="<?php echo ($_POST['myoptions']); ?>" selected><?php echo ($_POST['myoptions']); ?></option>
    <?php
    }
    ?>
    <option value="option1">option1</option>
    <option value="option2">option2</option>
    <option value="option3">option3</option>
    <option value="option4">option4</option>
    <option value="option5">option5</option>
    </select>

    /* Tikila lets modify your code block to this */

    <?php
    if ( isset($_POST['myoptions'] )) { // Does the post variable myoption contain data ?
        
    $tmp $_POST['myoptions']; // Yes , put value in variable selected
    } else {
        
    $tmp ""// No, put null value in variable
    // End if

    DisplayMenuChoices$tmp ); // Display the menu, the correct choice will be selected
    ?>
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.


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
  •