<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <!-- Class examples from CS 328 - Week 9 Lecture 2 by: Sharon Tuttle last modified: 2026-03-25 you can run this using the URL: https:/nrs-projects.humboldt.edu/~st10/s26cs328/328lect09-2/328lect09-2.php note: validated by: * getting an .xhtml version of the executed result with the FORM by copying the resulting source from a browser's "show source" for this document with the form and pasting it into a file ending with -1.xhtml on nrs-projects * getting an .xhtml version of the executed result with the FORM's RESPONSE by copying the resulting source from a browser's "show source" for this document with the form's response and pasting it into a file ending with -2.xhtml on nrs-projects * validating those two .xhtml versions using one of our usual validator links --> <head> <title> for CS 328 Week 9 Lecture 2 </title> <meta charset="utf-8" /> <link href="https://nrs-projects.humboldt.edu/~st10/styles/normalize.css" type="text/css" rel="stylesheet" /> </head> <body> <h1> Our Second PHP document - a postback PHP! </h1> <?php $play_val = 42; ?> <ul> <li> Value of $play_val: <?= $play_val ?> </li> <li> PHP type of $play_val: <?= gettype($play_val) ?> </li> <li> Is $play_val of type integer? <?= is_int($play_val) ?> </li> <li> Is $play_val of type string? <?= is_string($play_val) ?> </li> </ul> <?php /* examples of two ways to define a PHP named constant */ const MAX_TEMP = 100; define("MIN_TEMP", 0); ?> <p> Legal temperature range I want is: [<?= MIN_TEMP ?>, <?= MAX_TEMP ?>] </p> <p> <?php $pet = "dog"; $companion = 'dog'; /* example of a PHP if statement */ if ($pet == $companion) { ?> My pet and companion is a <?= $pet ?>! <?php } else { ?> Please don't let me ever reach this... <?php } ?> </p> <hr /> <?php /*=== HERE's the postback part -- either produces a result including a form, or including the response TO that form's submission, depending on the method used for requesting this PHP document (whether its URL was entered into a browser, which by default has method of "get", or whether a form was submitted with its URL as its action and method="post") ===*/ if ($_SERVER["REQUEST_METHOD"] == "GET") { /*=== if request method was "get", include a form in the PHP's result ===*/ ?> <form method="post" action="<?= htmlentities($_SERVER["PHP_SELF"], ENT_QUOTES) ?>"> <label for="nick"> Enter a nickname: </label> <input type="text" name="nickname" id="nick" /> <br /> <input type="submit" /> </form> <?php } else { // assume request method was "post" -- that this // request was from submitted form's action // with method="post" // assuming no nickname should have tags in it, // and am stripping any out, as well as removing // any leading and trailing blanks ?> <p> Welcome <?= trim(strip_tags($_POST["nickname"])) ?>.</p> <?php } ?> <footer> <hr /> <p> Validate by pasting .xhtml copy's URL into<br /> <a href="https://validator.w3.org/nu"> https://validator.w3.org/nu </a> or <a href="https://html5.validator.nu/"> https://html5.validator.nu/ </a> </p> </footer> </body> </html>