Newbie help needed with checking for posted data

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 ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-06-2004
Miffed
 
Posts: n/a
Default Newbie help needed with checking for posted data

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?


Reply With Quote
  #2 (permalink)  
Old 09-06-2004
Colin McKinnon
 
Posts: n/a
Default Re: Newbie help needed with checking for posted data

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.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 01:42 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0