This also works with the ternary operator... I don't think it likes the ++ because it doesn't know the type yet.
<?php $numtoguess=42; $num_tries = isset($_POST['num_tries'])?$_POST['num_tries']+1:0; $message=""; if(!isset($_POST['guess'])) $message="Welcome to the guessing machine"; elseif ($_POST['guess'] < $numtoguess) $message="Your guess was too low."; elseif ($_POST['guess'] > $numtoguess) $message="Your guess was too high."; else $message="That is the correct number."; if(isset($_POST['guess'])) $guess=(int)$_POST['guess']; else $guess=0; print_r($_POST); ?> <HTML> <BODY> <H1> <?php print $message ?> </H1> Guess number: <?php print $num_tries ?> <FORM METHOD="post"> Type your guess here:<BR> <INPUT TYPE ="text" NAME="guess" VALUE="<?php print $guess ?>"> <INPUT TYPE ="hidden" NAME="num_tries" VALUE="<?php print $num_tries ?>"> <INPUT TYPE="submit" VALUE="submit it!"> </FORM> </BODY> </HTML> *********** REPLY SEPARATOR *********** On 04/01/2003 at 6:40 PM Anthony Ritter wrote: >The following script is found in Matt Zandstra's book on PHP (Sams) on >page >160. > >He's explaining forms with PHP and has hard coded a value - 42. > >He also is explaining a hidden fleld where the user can submit the number >and each time the user hits submit in the formbox, the counter will >register >the total number of guesses. > >However, after running this script and submitting the number, I get: > >Guess number: 0 \\\"\\ > >submit again and... > >Guess number: 0 \\\\\\""\\\\, etc... > >instead of: > >Guess number: 1 >Guess numver : 2 >etc... > >Thanking all in advance. >Tony Ritter >.......................................................... > > ><? >$numtoguess=42; >$num_tries=(isset($num_tries)) ? $num_tries++ : 0; >$message=""; >if(!isset($guess)) >$message="Welcome to the guessing machine"; >elseif ($guess<$numtoguess) >$message="Your guess was too low."; >elseif ($guess>$numtoguess) >$message="Your guess was too high."; >else >$message="That is the correct number."; >$guess=(int)$guess; >?> ><HTML> ><BODY> ><H1> ><? print $message ?> ></H1> >Guess number: <?print $num_tries?> ><FORM METHOD="post"> >Type your guess here:<BR> ><INPUT TYPE ="text" NAME="guess" VALUE="<?print $guess?>"> ><INPUT TYPE ="hidden" NAME="num_tries" VALUE=<? print $num_tries?>"> ><INPUT TYPE="submit" VALUE="submit it!"> ></FORM> ></BODY> ></HTML> > > > > > >-- >PHP General Mailing List (http://www.php.net/) >To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php