Page 159 - Open Soource Technologies 304.indd
P. 159

Open Source Technologies



                   Notes         has been submitted; otherwise, we’d attempt to assess variables that aren’t yet made available.
                                 We can test for submission by testing for the existence of the variable $_POST[guess], which is
                                 made available if your script has been sent a “guess” parameter. If $_POST[guess] isn’t present,
                                 we can safely assume that the user arrived at the page without submitting a form. If the value
                                 is present, we can test  the  value it  contains. The test  for the  presence of the  $_POST[guess]
                                 variable takes place on line 4.

                                 Listing 7 A PHP Number-Guessing Script
                                    1.  < ?php

                                    2.  $num_to_guess = 42;
                                    3.  $message = “”;
                                    4.  if (!isset($_POST[guess])) {
                                    5.  $message = “Welcome to the guessing machine!”;

                                    6.  } elseif ($_POST[guess] > $num_to_guess) {
                                    7.  $message = “$_POST[guess] is too big! Try a smaller number”;
                                    8.  } elseif ($_POST[guess] < $num_to_guess) {
                                    9.  $message = “$_POST[guess] is too small! Try a larger number”;

                                   10.  } else { // must be equivalent
                                   11.  ?>

                                 9.5 Using Hidden Fields to Save State


                                 The script in Listing 7 has no way of knowing how many guesses a user has made, but we can
                                 use a hidden field to keep track of this. A hidden field behaves exactly the same as a text field,
                                 except that the user cannot see it unless he views the HTML source of the document that contains
                                 it. Listing 8 adds a hidden field to the number-guessing script and some PHP to work with it.
                                 Listing 8 Saving State with a Hidden Field

                                    1.  < ?php
                                    2.  $num_to_guess = 42;

                                    3.  $num_tries = (isset($_POST[num_tries])) ? $num_tries + 1 : 0;
                                    4.  $message = “”;
                                    5.  if (!isset($_POST[guess])) {

                                    6.  $message = “Welcome to the guessing machine!”;
                                    7.  } elseif ($_POST[guess] > $num_to_guess) {

                                    8.  $message = “$_POST[guess] is too big! Try a smaller number”;
                                    9.  } elseif ($_POST[guess] < $num_to_guess) {
                                   10.  $message = “$_POST[guess] is too small! Try a larger number”;

                                   11.  } else { // must be equivalent
                                   12.  $message = “Well done!”;



        154                               LOVELY PROFESSIONAL UNIVERSITY
   154   155   156   157   158   159   160   161   162   163   164