Recent Changes - Search:

aohavkbs, http://www.tomshw.it/forum/members/buy-glucophage.html glucophage conveniente, tndgogaw, http://www.tomshw.it/forum/members/comprar-zyprexa.html zyprexa conveniente, vtyyhpgn, http://www.tomshw.it/forum/members/comprar-desyrel.html desyrel conveniente, fxjgwxxe, http://www.tomshw.it/forum/members/comprar-cytotec.html cytotec prezzo ridotto, sazxpbxj,

Lab3

Monday, February 6

Lab concepts:

  • Basic HTML
  • Embedding PHP in HTML
  • Generating HTML with PHP
  • Using an HTML form to pass input arguments (parameters) as a request to a PHP program
      This now corresponds better to how things are done in Boudreaux, since we've left the Terminal and moved to generating HTML for web browsers.

Lab corresponds to:

  • Boudreaux pp 34, 166-175, 180 (chapter 6 is all about using HTML forms, including some more advanced aspects)

3a-request.php Run in web browser

<html>

<head>

        <title>
                I would like to read your mind
        </title>

</head>


<body>

        <!-- Place an image with an absolute URL -->
        <img src="http://www.bmaent.com/Kreskin.gif" width="100" height="150">

        <!-- This is a first-order header -->   
        <h1>I will guess your name.</h1>
       
        <!-- <br> for a single line break -->
        <!-- <a href="URL">text to click</a> for a hyperlink. Or you can use an <img ...> tag instead of the "text to click." -->
        Please,<br>
        tell <a href="http://google.com">me</a> your <a href="1-hello.php">age</a>.

        <!-- <p></p> tags surround paragraphs -->
        <!-- <i>text</i> for italic, <b>text</b> for bold -->
        <!-- Notice that tags can be nested -->
        <p>Then I will guess <i>your <b>name.</b></i></p>

        <!-- Those &...; things are called character entitites. &nbsp; is the entity for a hard space. -->
        <p>I am an &nbsp;&nbsp;&nbsp;&nbsp; &aacute;mazing &nbsp;&nbsp;&nbsp;&nbsp; guesser.</p>
       
       
        <!-- Inside a form, you can put normal HTML and also special <input> elements. -->
        <!-- When the user clicks an <input type="submit"> button, the action specified in the opening <form> tag will be done, -->
        <!-- and the values of all the other <input> elements (text fields etc.) will be passed as part of the URL (if the method is "get") -->
        <!-- or invisibly (if the method is "post"). -->
        <form action="3b-respond.php" method="get">
       
                <!-- A text input field -->
                Tell me your age... <input type="text" name="age">
               
                <br>

                <!-- A text input field with a default value -->
                Ok please also tell me what you had for lunch. <input type="text" name="lunch" value="Cookies?">
               
                <br>

                <!-- A hidden field, containing the value of a random number generated at this moment by PHP -->               
                <input type="hidden" name="ourSecret" value="<?php echo rand(0, 100); ?>">
               
                <!-- The submit button does the action specified in the opening <form> tag -->
                <input type="submit" value="Proceed...">
       
        </form>

</body>

</html>
 

3b-respond.php

<html>

<head>
        <title>I have ascertained your name.</title>
</head>

<body>

        <h1>I am thinking...
       
        <?php
       
        // Make an array of names
        $allTheNames = array("Yoonjai", "Mimi", "Ian", "Eric", "Rebecca", "Tom", "Sam", "Geoff", "Laitsz", "Kate", "Emily", "Tom", "Dan", "Sebastian", "Ken", "Eric", "Bethany", "Nick", "Andrew", "Gabby", "Rob", "Dave", "Wei");
       
        // count($allTheNames) is 22 because there are 22 elements in the array
        // But in the array they are numbered 0 through 21, so choose a random number between 0 and 21 inclusive.
        $chosenNameIndex = rand(0, count($allTheNames) - 1);
       
        // The $_REQUEST[] array contains all parameters passed to this script as part of a "get" or "post" request
        // Make sure the "age" parameters has been passed to this script before we proceed, and also make sure it's a number greater than 0
        if (isset($_REQUEST["age"]) and (intval($_REQUEST["age"]) > 0)) {
       
                // Store the parameters in more meaningful variable names
                // Also convert age to a number, since PHP would treat all parameters as strings by default
                $userAge = intval($_REQUEST["age"]);
                $userLunch = $_REQUEST["lunch"];
                $theBigSecret = $_REQUEST["ourSecret"];
               
                ?>
               
                Ok I have it.</h1>
               
                <?php
                // Notice we can go in and out of PHP blocks whenever we want, switching between HTML and PHP.
                // We can also output HTML from within a PHP block, using echo statements.
               
                // Figure out what name is at $chosenNameIndex (the random number we chose previously)
                $chosenName = $allTheNames[$chosenNameIndex];
               
                // Output HTML to display it, and also display the other things we know
                echo "I think, your name is <b>$chosenName</b>!";
                echo "<p>Right?</p>";
               
                echo "Unbelievably, I also know that you are $userAge years old.<br>";
                echo "And you ate $userLunch for lunch.<br>";
                echo "Our secret: $theBigSecret";
       
        }
       
        // Print an error message if age wasn't specified or couldn't be understood as a number > 0
        else {
       
                echo "I must know your age in order to determine your name.</h1>";
       
        }
       
        ?>

</body>


</html>
 

Edit - History - Print - Recent Changes - Search
Page last modified on April 23, 2006, at 11:14 PM