Ernest's All in One form
Results
Array ( )
Array ( )
<?php
include('../includes/header.php');
$self = htmlspecialchars(basename($_SERVER['SCRIPT_NAME']));
// populate the fruit array
$fruit = array("Apple", "Pear", "Orange", "Mango", "Apricot", "Pineapple");
// populate vars from POST
$submit = isset($_POST['submit']) ? $_POST['submit'] : null;
$first_name = isset($_POST['first_name']) ? $_POST['first_name'] : null;
$last_name = isset($_POST['last_name']) ? $_POST['last_name'] : null;
$full_time = isset($_POST['full_time']) ? $_POST['full_time'] : null;
$admin = isset($_POST['admin']) ? $_POST['admin'] : null;
// populate choice from radio input
if (isset($_POST['choice'])) {
$choice = $_POST['choice'];
} else {
$choice = '';
}
// populate option array from select control
$option = array();
if (isset($_POST['option'])) {
foreach ($_POST['option'] as $value) {
$option[] = $value;
}
}
// populate option2 array from multi-select control
$option2 = array();
if (isset($_POST['option2'])) {
foreach ($_POST['option2'] as $value) {
$option2[] = $value;
}
}
// set what was checked
$s = ' SELECTED ';
$c = ' CHECKED ';
$full_time = ($full_time == 'on') ? $c : '';
$admin = ($admin == 'on') ? $c : '';
?>
<h1>Ernest'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>
<br>
<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>
<br>
<fieldset>
<legend>Radio:</legend>
<p>Fruit</p>
<?php foreach ($fruit as $f): ?>
<input type="radio" name="choice" value="<?php echo $f; ?>"
<?php if ($choice == $f) echo($c); ?>> <?php echo $f; ?><br>
<?php endforeach; ?>
</fieldset>
<br>
<fieldset>
<legend>Dropdown:</legend>
<label for="option[]">Fruit</label>
<br>
<select id="option[]" 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>
<br>
<fieldset>
<legend>Multiple:</legend>
<label for="option2[]">Fruit</label>
<br>
<select id="option2[]" 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>
<br>
<h2>Results</h2>
<br>
<?php
echo('<pre>');
print_r($_POST);
echo('</pre>');
include('../includes/footer.php');
?>