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! |
David Barnes, Lead Evangelist for IBM Emerging Internet Technologies will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve. FREE! Go There Now!
|
|
|
|
WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of Lotus Quickr 8.0, which enables collaboration by transforming the way everyday business content such as documents, rich media, photos, and video can be shared. Lotus Quickr makes it faster and easier to share content of all types (not just documents) within virtual teams. It is designed to make it easier to collaborate across organizational boundaries, while continuing to work within the context of familiar desktop applications. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of WebSphere Business Modeler Advanced V6.1.1, IBM’s premier business process modeling and analysis tool for business users that offers process modeling, simulation, and analysis capabilities. IBM WebSphere Business Modeler helps you visualize, understand, and document business processes for continuous improvement. FREE! Go There Now!
|
|
|
|
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!
|
|
|
|
Join this webcast to see how IBM Data Studio Developer and pureQuery can take the pain out of Java data access. uApplications developed using both Java and SQL have become a common requirement. Database connectivity using Java Database Connectivity (JDBC) to create an application is a multi-step tedious process, and tooling that covers both SQL and Java has been unavailable, until now. IBM Data Studio introduces the pureQuery platform: a high-performance, Java data access platform focused on simplifying the tasks of developing, managing, and optimizing database applications and services. FREE! Go There Now!
|
|
|
|
Secure your Web applications with IBM Rational AppScan Standard Edition V7.7, previously known as Watchfire AppScan. This Web application security testing tool automates vulnerability assessments and scans and tests for common Web application vulnerabilities. Visit IBM developerWorks to download a free trial of IBM Rational AppScan Standard Edition V7.7. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Functional Tester V7.0.1. Rational Functional Tester is an automated functional and regression testing solution for QA teams concerned with the quality of their Java, Microsoft Visual Studio .NET, and Web-based applications. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for process. The SOA Sandbox for process focuses on providing a trial environment with the necessary tooling and components required to gain a better understanding of business processes and how to best improve existing business processes to derive value quickly. FREE! Go There Now!
|
|
|
|
Explore how Rational and WebSphere software enable enterprise documentation in SOA environments. Specifically, a new integration between IBM WebSphere® Business Modeler and IBM Rational® Method Composer software can help technical writers more easily keep enterprise operations manuals in sync with changes that are made to business processes, resulting in more accurate and timely documentation that benefits the entire enterprise. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |