All in one form

Text
Checkbox
Radio
Dropdown
Multiple

Results

Array
(
)

Code follows

<?php

error_reporting
(E_ALL);    // set error reporting to all

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

// populate the positions array

$positions = array("Point Guard", "Shooting Guard", "Small Forward", "Power Forward", "Center");

// populate the vars from POST

$firstName = isset($_POST['firstName']) ? htmlentities($_POST['firstName']) : '';
$lastName  = isset($_POST['lastName'])  ? htmlentities($_POST['lastName'])  : '';

$starter = isset($_POST['starter']) ? ' checked ' : '';
$captain = isset($_POST['captain']) ? ' checked ' : '';

// the radio choice

$choice = isset($_POST['choice']) ? $_POST['choice'] : '';

// the dropdown and the multiple select both come back as arrays

$pick  = isset($_POST['pick'])  ? $_POST['pick']  : array();
$picks = isset($_POST['picks']) ? $_POST['picks'] : array();

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>All in one form</title>
    <link rel="stylesheet" href="css/styles.css">
</head>
<body>

<div class="page">

    <p class="nav"><a href="../index.php">Home</a></p>

    <h1>All in one form</h1>

    <form action="<?php echo($self); ?>" method="POST">

        <fieldset>
            <legend>Text</legend>

            <label for="firstName">First Name</label>
            <input type="text" id="firstName" name="firstName" size="30" value="<?php echo($firstName); ?>">

            <label for="lastName">Last Name</label>
            <input type="text" id="lastName" name="lastName" size="30" value="<?php echo($lastName); ?>">
        </fieldset>

        <fieldset>
            <legend>Checkbox</legend>

            <label class="opt"><input type="checkbox" name="starter" <?php echo($starter); ?>> Starter</label>
            <label class="opt"><input type="checkbox" name="captain" <?php echo($captain); ?>> Team Captain</label>
        </fieldset>

        <fieldset>
            <legend>Radio</legend>

            <label>Position</label>

            <?php foreach ($positions as $position) { ?>
                <label class="opt">
                    <input type="radio" name="choice" value="<?php echo($position); ?>"
                        <?php if ($choice == $position) { echo(' checked '); } ?>>
                    <?php echo($position); ?>
                </label>
            <?php } ?>
        </fieldset>

        <fieldset>
            <legend>Dropdown</legend>

            <label for="pick">Position</label>

            <select id="pick" name="pick[]">
                <?php foreach ($positions as $position) { ?>
                    <option value="<?php echo($position); ?>"
                        <?php if (in_array($position, $pick)) { echo(' selected '); } ?>>
                        <?php echo($position); ?>
                    </option>
                <?php } ?>
            </select>
        </fieldset>

        <fieldset>
            <legend>Multiple</legend>

            <label for="picks">Positions (hold Ctrl or Command to pick more than one)</label>

            <select id="picks" name="picks[]" size="5" multiple>
                <?php foreach ($positions as $position) { ?>
                    <option value="<?php echo($position); ?>"
                        <?php if (in_array($position, $picks)) { echo(' selected '); } ?>>
                        <?php echo($position); ?>
                    </option>
                <?php } ?>
            </select>
        </fieldset>

        <input type="submit" name="submit" value="Submit">

    </form>

    <h2>Results</h2>

    <pre><?php print_r($_POST); ?></pre>

    <hr>

    <h2>Code follows</h2>

    <div class="code"><?php highlight_file(__FILE__); ?></div>

    <p class="nav nav-foot"><a href="../index.php">Home</a></p>

</div>

</body>
</html>