Miscellaneous Code

  Home arrow Miscellaneous Code arrow make select menu that remembers users ...
MISCELLANEOUS CODE

make select menu that remembers users choice
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2004-09-27

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    this function will create an html select menu. It will "remember" the users choice. for example, lets say the select menu has 3 choices,
    red,
    green,
    and blue

    if the user chooses green, and leaves the page, then comes back later, the select menu will already be set to green again. It basicaly just adds selected="selected" to the correct option.

    this is very trivial but handy as hell when making websites.

    this demo uses sessions, but you dont absolutely need to.

    As with ALL of the code snippits online, you should take a moment to understand how they work. Otherwise your likely to have problems sooner or later.

    This is just an example, you may need to fine tune it to your needs.

    By : rehfeld_dot_us

    <?php

    function makeSelectMenu($menuName, $options, $chosen) {
    $list = '';
    foreach ($options as $option => $text) {
    $selected = ($chosen == $option) ? ' selected="selected"' : '';
    $list .= " <option value=\"$option\"$selected>$text</option>\n";
    }
    return "\n<select name=\"$menuName\" id=\"$menuName\">\n$list</select>\n";
    }


    // example usage

    session_start();


    // here we define the option names, and the corresponding text to be displayed for that option
    // for example <option value="red">I like Red</option>
    $options = array(
    'red' => 'I like Red',
    'blue' => 'I like Blue',
    'green' => 'I like Green'
    );

    // this will be the <select name="favoriteColor" value in the html select list
    $menuName = 'favoriteColor';



    // if they submitted the form, lets assign thier selection to the session
    if (isSet($_POST[$menuName])) {
    $_SESSION[$menuName] = $_POST[$menuName];
    }




    // $chosen will obviously be filled with the users choice
    if (isSet($_SESSION[$menuName])) {
    $chosen = $_SESSION[$menuName];
    } else {
    $chosen = '';
    }


    $selectMenu = makeSelectMenu($menuName, $options, $chosen);



    /*
    this above example will output the following:

    <select name="favoriteColor" id="favoriteColor">
    <option value="red">I like Red</option>
    <option value="blue">I like Blue</option>
    <option value="green">I like Green</option>
    </select>

    and if the user selects "green", this would be the output

    <select name="favoriteColor" id="favoriteColor">
    <option value="red">I like Red</option>
    <option value="blue">I like Blue</option>
    <option value="green" selected="selected">I like Green</option>
    </select>





    HERES HOW SMALL THE CODE IS w/out comments:

    $options = array(
    '' => 'Choose Your Favorite Color',
    'red' => 'I like Red',
    'blue' => 'I like Blue',
    'green' => 'I like Green'
    );
    $menuName = 'favoriteColor';
    $chosen = '';
    if (isSet($_POST[$menuName])) $_SESSION[$menuName] = $_POST[$menuName];
    if (isSet($_SESSION[$menuName])) $chosen = $_SESSION[$menuName];
    $selectMenu = makeSelectMenu($menuName, $options, $chosen);








    */

    ?>
    <html>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

    <?php echo $selectMenu; ?>
    <input type="submit" />
    </form>


    </html>
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Miscellaneous Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap