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
developerWorks - FREE Tools! |
Download a free trial version of IBM Rational Software Analyzer Developer Edition V7.0 to identify bug defects earlier in the software development cycle. Rational Software Analyzer is an extensible software development solution that reduces the expense of bug-fixes by enabling static analysis code reviews and bug identification very early in the development cycle. FREE! Go There Now!
|
|
|
|
In this tutorial, you can learn how to install and configure the IBM Rational Asset Manager Eclipse client, explore the different views in the Asset Management perspective, learn various search techniques, work with existing assets, and submit a new asset. FREE! Go There Now!
|
|
|
|
Learn from the best! Find out how developers use Rational ClearCase to be more flexible, innovative and deliver higher quality code in the Rational ClearCase Power Users eKit. This complimentary eKit provides a collection of materials, like articles, whitepapers, and demos that can help you become a power user of Rational ClearCase. FREE! Go There Now!
|
|
|
|
As systems increase in complexity, communication between systems and software teams becomes more and more difficult. Now, there’s a way to improve product quality and communication.<br />Read the “Model Driven Systems Development” white paper to see how. Also included in this kit are more educational white papers, customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems.<br /> FREE! Go There Now!
|
|
|
|
Listen to this webcast to get an overview of Info 2.0 and a technical demo of how to quickly build an enterprise mashup. IBM's Info 2.0 technology leverages emerging Web 2.0 technologies such as mashups, feeds, AJAX, and JSON in order to simplify assembly of information using feeds and services. Come learn about the technical elements of Info 2.0 including the Feed Generation framework, Mashup Engine, and mashup assembly components. Learn how to pull information from databases, departmental information, and the Web to create mashups critical to your company’s success. We will also discuss best practices to help you get started. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 6 at 1:00 pm ET to participate in an agile application development discussion and get your questions answered on using IBM Rational Method Composer in a distributed environment.Get your questions answered! FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Method Composer V7.2 which helps you deliver customized yet consistent process guidance to your project teams and IT organization, and includes the latest version of IBM Rational Unified Process (RUP), which has provided process guidance to teams since 1996. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Tester for SOA Quality V7.0.1, a functional and regression testing tool that enables the creation, comprehension, modification and execution of testing GUI-less Web services. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for people. The SOA Sandbox for people provides a trial environment with the necessary tooling and components required to enable consistent human and process interaction and collaboration, showing how you can improve user experience and business productivity. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference, to hear how Enterprise Generation Language (EGL) eliminates the need for tedious and error-prone low level coding, so developers can focus on business requirements. EGL extends the Rational software development platform with a simplified programming language that enables developers who have little or no experience with Java, Web technologies or Service Oriented Architecture, to create enterprise-class applications and services quickly and easily. It also allows developers who may have little or no mainframe programming experience to quickly create traditional mainframe components. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |