This is a discussion on Newbie help needed with checking for posted data within the alt.comp.lang.php forums, part of the PHP Programming Forums category; It's a bit of a cliche, but I'm an experienced programmer, I'm only new to php (and ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
It's a bit of a cliche, but I'm an experienced programmer, I'm only new to
php (and web programming in general to be honest). Anyway, been using "TY PHP in 24hrs" without too many problems, until I came across the following code: <?php $num_to_guess = 42; $message = ""; $num_tries = ( isset( $num_tries ) ) ? ++$num_tries : 0; //print("numtries= " . $num_tries); // I've added this line if ( ! isset( $_POST["guess"] ) ) { $message = "Welcome to the guessing machine!"; } elseif ( $_POST["guess"] > $num_to_guess ) { $message = $_POST["guess"] . " is too big! Try a smaller number"; } elseif ( $_POST["guess"] < $num_to_guess ) { $message = $_POST["guess"] . " is too small! Try a larger number"; } else { // must be equivalent $message = "Well done!"; } $_POST["guess"] = (int) $_POST["guess"]; ?> <html> <head> <title>Listing 9.11 Saving state with a hidden field</title> </head> <body> <h1><?php print $message ?></h1> Guess number: <?php print $num_tries?> <form action="<?php print $_SERVER["PHP_SELF"]?>" method="POST"> Type your guess here: <input type="text" name="guess" value="<?php print $_POST["guess"]?>"> <input type="hidden" name="num_tries" value="<?php print $num_tries?>"> </form> </body> </html> It's just a very simple 'guess the number' game (so simple, the number is hard-coded). The only 'interesting' thing to note is that the html form code calls itself using PHP_SELF. The only other point of interest is that the original code doesn't prefix PHP_SELF with $_SERVER as I have had to do (otherwise it won't comple for me, ditto for all the references to the 'guess' variable). The code does work EXCEPT for the 'num_tries' bit. I know from a previous example that data isn't posted when the field is empty(or is it?) which is why (I assume) the isset() function is used to set num_tries to zero when nothing is received. So why doesn't this work second time around? |
|
|||
|
Miffed spilled the following:
<snip> > It's just a very simple 'guess the number' game (so simple, the number is > hard-coded). The only 'interesting' thing to note is that the html form > code calls itself using PHP_SELF. The only other point of interest is > that the original code doesn't prefix PHP_SELF with $_SERVER as I have had > to do (otherwise it won't comple for me, ditto for all the references to > the 'guess' variable). > compile? > The code does work EXCEPT for the 'num_tries' bit. I know from a previous > example that data isn't posted when the field is empty(or is it?) which is > why (I assume) the isset() function is used to set num_tries to zero when > nothing is received. So why doesn't this work second time around? At a guess because your installation has register_globals=no (as recommended) in the config file - this means that values obtained from CGI are not mapped as global variables (e.g. $PHP_SELF, $num_tries) but are available from the relevant superglobals array ($_SERVER['PHP_SELF'], $_REQUEST['num_tries'] or $_POST['num_tries'] HTH C. |