Steph's All in One Form Selection

Text Fields

Checkbox Fields

Radio Options Select Country

Dropdown Selection

Multiple Choice Selection


Results


Array
(
)

Source Code

<?php
include ('../header/assignmentHeader.php');

$self = htmlspecialchars(basename($_SERVER['SCRIPT_NAME']));

// Array for country selection
$countries = array("United States", "Canada", "United Kingdom", "Australia", "Germany", "Japan", "Brazil");

// Populate vars from POST using null coalescing operators, which makes it a little cleaner/handles empty state better
$submit     = $_POST['submit'] ?? null;
//Input
$first_name = $_POST['first_name'] ?? '';
$last_name  = $_POST['last_name'] ?? '';

//check and drop down
$full_time  = $_POST['full_time'] ?? '';
$admin      = $_POST['admin'] ?? '';
$choice     = $_POST['choice'] ?? '';

// Grab selected options from POST arrays directly
$option  = $_POST['option'] ?? [];
$option2 = $_POST['option2'] ?? [];

$s = ' selected '; // Helper string for selected active state
$c = ' checked '; // Helper string for checked active state

// Convert checkbox states into HTML checked attributes allows it to hold the state  better
$full_time_checked = ($full_time === 'on') ? $c : '';
$admin_checked     = ($admin === 'on') ? $c : '';
?>

<!--// 1. Create variables and helper-->
<!--// 2. Create reference and html elements-->
<!--// 3. Create form behavior for each input -->

<link rel="stylesheet" href="../css/week3Assignment.css" />
<link rel="stylesheet" href="../css/assignmentHeaderStyles.css" />

<h1>Steph's All in One Form Selection</h1>

<form action="<?php echo $self; ?>" method="post" class="formContainerVariant">
    <fieldset>
        <legend>Text Fields</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>
        <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 Fields</legend>
        <div class="row-layout">
            <input type="checkbox" id="full_time" name="full_time" <?php echo $full_time_checked; ?>>
            <label for="full_time">Full Time</label>
        </div>
        <div class="row-layout">
            <input type="checkbox" id="admin" name="admin" <?php echo $admin_checked; ?>>
            <label for="admin">Admin</label>
        </div>
    </fieldset>
    <br>

    <fieldset>
        <legend>Radio Options</legend>
        <span class="label-title">Select Country</span><br>
        <?php foreach ($countries as $item): ?>
            <div class="row-layout">
                <input type="radio" id="radio_<?php echo $item; ?>" name="choice" value="<?php echo $item; ?>" <?php echo ($choice === $item) ? $c : ''; ?>>
                <label for="radio_<?php echo $item; ?>"><?php echo $item; ?></label>
            </div>
        <?php endforeach; ?>
    </fieldset>
    <br>

    <fieldset>
        <legend>Dropdown Selection</legend>
        <label for="single-option">Select Favorite Country</label>
        <br>
        <select id="single-option" name="option[]">
            <?php foreach ($countries as $item): ?>
                <option value="<?php echo $item; ?>" <?php echo in_array($item, $option) ? $s : ''; ?>>
                    <?php echo $item; ?>
                </option>
            <?php endforeach; ?>
        </select>
    </fieldset>
    <br>

    <fieldset>
        <legend>Multiple Choice Selection</legend>
        <label for="multiple-option">Select Countries you want to visit</label>
        <br>
        <select id="multiple-option" name="option2[]" multiple>
            <?php foreach ($countries as $item): ?>
                <option value="<?php echo $item; ?>" <?php echo in_array($item, $option2) ? $s : ''; ?>>
                    <?php echo $item; ?>
                </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>';

echo 
'<br><h2>Source Code</h2>';
show_source(__FILE__);

include (
'../footer/footer.php');
?>