Using Hidden Fields to Save State

The script in Listing 10.7 has no way of knowing how many guesses a user has made. We can use a hidden field to keep track of this. The mark-up for a hidden field is similar to that of a text field. From the user’s perspective, however, it has no output. A user cannot see a hidden field, unless he views the HTML source of the document that contains it. Listing 10.8 adds a hidden field to the number-guessing script and some PHP to work with it.

Listing 10.8. Saving State with a Hidden Field
 1: <?php 2: $num_to_guess = 42; 3: $message = ""; 4: if ( ! isset( $_POST['guess'] ) ) { 5: $message = "Welcome to the guessing machine!"; 6: } else if ( $_POST['guess'] > $num_to_guess ) { 7: $message = $_POST['guess']." ...

Get Sams Teach Yourself PHP in 24 Hours, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.