<?php
// Include the header file
include('includes/header.php');
// Set error reporting to all for development
error_reporting(E_ALL);
// Get the current script name and escape it for security
$self = htmlspecialchars(basename($_SERVER['SCRIPT_NAME']));
// Initialize the fruit array
$fruit = array("Apple", "Pear", "Orange", "Mango", "Apricot", "Pineapple");
// Populate variables from POST request
$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;
$part_time = isset($_POST['part_time']) ? $_POST['part_time'] : null;
// Populate the selected choice from radio input
$choice = isset($_POST['choice']) ? $_POST['choice'] : '';
// Populate the selected options from the dropdown
$option = isset($_POST['option']) ? $_POST['option'] : array();
$option2 = isset($_POST['option2']) ? $_POST['option2'] : array();
// Set 'checked' and 'selected' attributes for checkboxes and dropdowns
$c = ' CHECKED ';
$s = ' SELECTED ';
$full_time = ($full_time == 'on') ? $c : '';
$admin = ($admin == 'on') ? $c : '';
$part_time = ($part_time == 'on') ? $c : '';
?>
<h1>Don Alexander Eckford'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 htmlspecialchars($first_name); ?>">
<br><br>
<label for="last_name">Last Name</label>
<input type="text" size="30" id="last_name" name="last_name" value="<?php echo htmlspecialchars($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><br>
<label for="admin">Admin</label>
<input type="checkbox" id="admin" name="admin" <?php echo $admin; ?>>
<br><br>
<label for="part_time">Part Time</label>
<input type="checkbox" id="part_time" name="part_time" <?php echo $part_time; ?>>
</fieldset>
<br>
<fieldset>
<legend>Radio:</legend>
<label for="choice">Fruit</label>
<br><br>
<?php foreach ($fruit as $item): ?>
<label for="choice_<?php echo strtolower($item); ?>">
<input type="radio" id="choice_<?php echo strtolower($item); ?>" name="choice" value="<?php echo $item; ?>" <?php if ($choice == $item) { echo $c; } ?>> <?php echo $item; ?>
</label>
<br><br>
<?php endforeach; ?>
</fieldset>
<br>
<fieldset>
<legend>Dropdown:</legend>
<label for="option">Fruit</label>
<br><br>
<select id="option" name="option[]">
<?php foreach ($fruit as $item): ?>
<option value="<?php echo $item; ?>" <?php if (in_array($item, $option)) { echo $s; } ?>><?php echo $item; ?></option>
<?php endforeach; ?>
</select>
</fieldset>
<br>
<fieldset>
<legend>Multiple:</legend>
<label for="option2">Fruit</label>
<br><br>
<select id="option2" name="option2[]" multiple>
<?php foreach ($fruit as $item): ?>
<option value="<?php echo $item; ?>" <?php if (in_array($item, $option2)) { echo $s; } ?>><?php echo $item; ?></option>
<?php endforeach; ?>
</select>
</fieldset>
<br>
<input type="submit" name="submit" value="Submit">
</form>
<br>
<h2>Results</h2>
<br>
</div> <!-- end of content -->
<div id="footer">
<p>
• Copyright© <?php echo(date("Y")); ?> Don Alexander Eckford (Dxander, DxanderDev)
</p>
</div>
</div> <!-- end of page -->
<?php
// Display the results
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
include('includes/footer.php');
?>