Core PHP

User Selections in Cookies

This PHP example program demonstrates how make selections in form and pass values as cookies.

Selections.html

<!DOCTYPE html>
<html>
	<head>
		<title>XoaX.net: PHP</title>
	</head>
	<body>
		<form  action="ColorPreferences.php" method="post">
			<fieldset style="width:200px">
				<legend>Color Preferences</legend>
				Background:
				<select name="nBackground">
					<option value="black">Black</option>
					<option value="yellow">Yellow</option>
					<option value="orange">Orange</option>
					<option value="magenta">Magenta</option>
					<option value="white">White</option>
					<option value="red">Red</option>
					<option value="blue">Blue</option>
				</select><br /><br />
				Foreground:
				<select name="nForeground">
					<option value="black">Black</option>
					<option value="yellow">Yellow</option>
					<option value="orange">Orange</option>
					<option value="magenta">Magenta</option>
					<option value="white">White</option>
					<option value="red">Red</option>
					<option value="blue">Blue</option>
				</select><br /><br />
				<input type="submit" value="Set Color Preferences">
			</fieldset>
		</form>
	</body>
</html>
 

ColorPreferences.php

<?php
	$saColors = array('black' => '#000000',
		'yellow' => '#ffff00',
		'orange' => '#ff8000',
		'magenta' => '#ff00ff',
		'white' => '#ffffff',
		'red' => '#ff0000',
		'blue' => '#0000ff');
	$sBackgroundName = $_POST['nBackground'];
	$sForegroundName = $_POST['nForeground'];
	setcookie('sBackgroundColor', $saColors[$sBackgroundName]);
	setcookie('sForegroundColor', $saColors[$sForegroundName]);
?>
<!DOCTYPE html>
<html>
	<head>
		<title>XoaX.net: PHP Set Color Preferences</title>
	</head>
	<body>
The color preferences have been changed to:<br />
Background: <?= $sBackgroundName ?><br />
Foreground: <?= $sForegroundName ?><br />
Click <a href="PreferenceDemo.php">here</a> to see the preferences in action.
	</body>
</html>
 

PreferenceDemo.php

<!DOCTYPE html>
<html>
	<head>
		<title>XoaX.net: PHP Color Preferences Demo</title>
	</head>
<?php
	$sBackground = $_COOKIE['sBackgroundColor'];
	$sForeground = $_COOKIE['sForegroundColor'];
?>
	<body bgcolor="<?= $sBackground ?>" text="<?= $sForeground ?>">
		<h1>Color Preferences Demonstration</h1>
		<p>How does it look? If you do not like it, click the link below to return to the the color
			selection screen and choose something else.</p>
		<span style="background-color:lightgray;color:black;"><a href="Selections.html">Click here</a> to return to the color selection.</span>
	</body>
</html>
 

Output

 
 

© 2007–2026 XoaX.net LLC. All rights reserved.