Page 224 - Open Soource Technologies 304.indd
P. 224
Web Technologies-I
Notes <option value=”black”>Black</option>
<option value=”white”>White</option>
<option value=”red”>Red</option>
<option value=”blue”>Blue</option>
</select><br/>
Foreground: <select name=”foreground”>
<option value=”black”>Black</option>
<option value=”white”>White</option>
<option value=”red”>Red</option>
<option value=”blue”>Blue</option>
</select>
<p /> <input type=”submit” value=”Change Preferences”>
</form>
</body>
</html>
The form in the above example submits to the PHP script prefs.php, which is shown in below
given example. This script sets cookies for the colour preferences specified in the form. Note
that the calls to setcookie( ) are made before the HTML page is started.
Setting preferences with cookies
<?php $colors = array(‘black’ => ‘#000000’, ‘white’ => ‘#ffffff’, ‘red’ => ‘#ff0000’, ‘blue’ =>
‘#0000ff’);
$bg_name = $_POST[‘background’];
$fg_name = $_POST[‘foreground’];
setcookie(‘bg’, $colors[$bg_name]);
setcookie(‘fg’, $colors[$fg_name]);
?>
<html>
<head>
<title>Preferences Set</title>
</head>
<body>Thank you. Your preferences have been changed to:<br />
Background: <?= $bg_name ?><br />
Foreground: <?= $fg_name ?><br />
Click <a href=”prefs-demo.php”>here</a> to see the preferences in action.
</body>
</html>
218 LOVELY PROFESSIONAL UNIVERSITY