<?php
    /*---
        function: make_lab14_form
        purpose: expects nothing, returns nothing,
            and outputs in its response a form allowing a user to enter
            several random things

        by: Sharon Tuttle
        last modified: 2026-04-30
    ---*/

    function make_lab14_form()
    {
        ?>

        <form action="<?= htmlentities($_SERVER["PHP_SELF"],
                                       ENT_QUOTES) ?>"
              method="post"
              id="lab_form">
            <fieldset>
                <legend> Values for today </legend>
                <label for="token">
                    A string with NO blanks: </label>
                <input type="text" name="token"
                       id="token" required="required" /> 

                <label for="even">
                    An even integer: </label>
                <input type="number" name="even"
                       id="even" required="required" /> 

                <div class="sub_button">
                    <input type="submit" value="submit entries" />
                </div>
            </fieldset>
        </form>
    <?php
    }

    /*---
        function: make_lab14_response
        purpose: expects nothing, returns nothing,
            and creates a response that responds to the
            submitted form with the user's entered values

        by: Sharon Tuttle
        last modified: 2026-04-30
    ---*/

    function make_lab14_response()
    {
        // CHECKING the passed values (because application
        //     tier MUST do this, no matter HOW much
        //     client-side checking is done -- anyone anywhere
        //     can write a DIFFERENT form with THIS PHP's
	//     URL as its action attribute value!!)

        // said can enter any string as long as no blanks in 1st field --
        //    so, making anything executable in there display-only

        $entered_token = htmlspecialchars($_POST["token"]);

        // but we'll check in a moment if 2nd field contained a number

        $entered_even = $_POST["even"];

        $complaint = "";

        // here, textfield's value should not contain a blank -- so first, make sure no
        //    blanks in $entered_token

        if ( ($entered_token == "") || (str_contains($entered_token, " ")) )
        {
            $complaint = "MUST enter something with NO blanks in first field! ";
        }

        // now, making sure number field contained an even integer

        if ( ( ((int) $entered_even) != $entered_even ) 
             || (($entered_even % 2) !== 0) )
        {    
            $complaint = $complaint .
                         " Second field must contain an even integer!";
        }

        if ($complaint != "")
        {
            ?>
            <p id="errors"> <?= $complaint ?> </p>

            <p> <a href="<?= htmlentities($_SERVER['PHP_SELF'], ENT_QUOTES) ?>">
                Try again </a> </p>
            <?php
        }
        else
        { 
            ?>
            <p> form's contents: </p>

            <!-- remember, we called htmlspecialchars on
                 the entered values above... -->

            <ul>
            <li> textfield's value: <?= $entered_token ?> </li>
            <li> number field's value: <?= $entered_even ?> </li>
            </ul>

            <p> <a href="<?= htmlentities($_SERVER['PHP_SELF'], ENT_QUOTES) ?>">
                Start Over </a> </p>
            <?php
        }
    }
?>