Kealin's All-in-One Form
CODE FOLLOWS
<?php
include('includes/header.php'); // use the working include path
$self = htmlspecialchars(basename($_SERVER['SCRIPT_NAME']));
// populate the fruit array
$fruit = array("Apple", "Pear", "Orange", "Mango", "Apricot", "Pineapple", "Watermelon");
// populate vars from POST
$submit = isset($_POST['submit']) ? $_POST['submit'] : null;
$first_name = isset($_POST['first_name']) ? $_POST['first_name'] : '';
$last_name = isset($_POST['last_name']) ? $_POST['last_name'] : '';
$full_time = isset($_POST['full_time']) ? $_POST['full_time'] : '';
$admin = isset($_POST['admin']) ? $_POST['admin'] : '';
$choice = isset($_POST['choice']) ? $_POST['choice'] : '';
$option = isset($_POST['option']) ? $_POST['option'] : array();
$option2 = isset($_POST['option2']) ? $_POST['option2'] : array();
// set what was checked/selected
$s = ' SELECTED ';
$c = ' CHECKED ';
$full_time = ($full_time == 'on') ? $c : '';
$admin = ($admin == 'on') ? $c : '';
?>
<!-- ===== start page content ===== -->
<div id="page">
<div id="content">
<h1>Kealin's All-in-One Form</h1>
<form action="<?php echo $self; ?>" method="post">
<fieldset>
<legend>Text:</legend>
<label for="first_name">First Name</label>
<input type="text" size="30" id="first_name" name="first_name" value="<?php echo $first_name; ?>">
<br>
<label for="last_name">Last Name</label>
<input type="text" size="30" id="last_name" name="last_name" value="<?php echo $last_name; ?>">
</fieldset>
<fieldset>
<legend>Checkbox:</legend>
<label for="full_time">Full Time</label>
<input type="checkbox" id="full_time" name="full_time" <?php echo $full_time; ?>>
<br>
<label for="admin">Admin</label>
<input type="checkbox" id="admin" name="admin" <?php echo $admin; ?>>
</fieldset>
<fieldset>
<legend>Radio:</legend>
<?php foreach ($fruit as $f): ?>
<label>
<input type="radio" name="choice" value="<?php echo $f; ?>" <?php if ($choice == $f) echo $c; ?>>
<?php echo $f; ?>
</label><br>
<?php endforeach; ?>
</fieldset>
<fieldset>
<legend>Dropdown:</legend>
<label for="option[]">Fruit</label>
<select name="option[]">
<?php foreach ($fruit as $f): ?>
<option value="<?php echo $f; ?>" <?php if (in_array($f, $option)) echo $s; ?>>
<?php echo $f; ?>
</option>
<?php endforeach; ?>
</select>
</fieldset>
<fieldset>
<legend>Multiple:</legend>
<label for="option2[]">Fruit</label>
<select name="option2[]" multiple>
<?php foreach ($fruit as $f): ?>
<option value="<?php echo $f; ?>" <?php if (in_array($f, $option2)) echo $s; ?>>
<?php echo $f; ?>
</option>
<?php endforeach; ?>
</select>
</fieldset>
<br>
<input type="submit" name="submit" value="Submit">
</form>
<?php if ($submit): ?>
<h2>Results</h2>
<pre><?php print_r($_POST); ?></pre>
<?php endif; ?>
</div> <!-- end of content -->
</div> <!-- end of page -->
<?php
include('includes/footer.php'); // working footer include with CODE FOLLOWS
?>