newbie needs help with checking for posted data

This is a discussion on newbie needs help with checking for posted data within the PHP Language 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 > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-06-2004
Miffed
 
Posts: n/a
Default newbie needs help 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" with PHP 4.3.6 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?

And before anyone says anything, yes I have just posted this in the alt.php
group, but I've only just seen a post referring to this much busier group.


Reply With Quote
  #2 (permalink)  
Old 09-06-2004
Janwillem Borleffs
 
Posts: n/a
Default Re: newbie needs help with checking for posted data

Miffed wrote:
> 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).
>


Well, point your browser to immediately to www.php.net then, most answers to
basic questions can be found there.

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


$num_tries should also be retrieved from the $_POST array when
register_globals is disabled:

$num_tries = ( isset( $_POST['num_tries'] ) ) ? ++$_POST['num_tries'] : 0;

> And before anyone says anything, yes I have just posted this in the
> alt.php group, but I've only just seen a post referring to this much
> busier group.


I haven't found your message there, so I won't accuse you of multi-posting
;-)


JW



Reply With Quote
  #3 (permalink)  
Old 09-06-2004
Janwillem Borleffs
 
Posts: n/a
Default Re: newbie needs help with checking for posted data

Janwillem Borleffs wrote:
> I haven't found your message there, so I won't accuse you of
> multi-posting ;-)
>


Did find your message in alt.comp.lang.php however. DO NOT multi-post, but
cross-post to several newsgroups at once as I did in this reply.


JW



Reply With Quote
  #4 (permalink)  
Old 09-06-2004
Michael Fesser
 
Posts: n/a
Default Re: newbie needs help with checking for posted data

.oO(Miffed)

In addition to the other reply:

>$_POST["guess"] = (int) $_POST["guess"];


There's no element 'guess' in the $_POST array when the script runs for
the first time, which causes a notice.

Micha
Reply With Quote
  #5 (permalink)  
Old 09-06-2004
Miffed
 
Posts: n/a
Default Re: newbie needs help with checking for posted data

"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:413cd916$0$7142$d5255a0c@news.euronet.nl...
> Janwillem Borleffs wrote:
> > I haven't found your message there, so I won't accuse you of
> > multi-posting ;-)
> >

>
> Did find your message in alt.comp.lang.php however. DO NOT multi-post, but
> cross-post to several newsgroups at once as I did in this reply.
>
>
> JW


I'm well aware of NG etiquette, thank you. Maybe I didn't explain clearly
that I only discovered this group after posting the first message.


Reply With Quote
  #6 (permalink)  
Old 09-06-2004
Miffed
 
Posts: n/a
Default Re: newbie needs help with checking for posted data

"Michael Fesser" <netizen@gmx.net> wrote in message
news:ljmpj0hud6ogtmlh60k5v0an3c7rrf71c4@4ax.com...
> .oO(Miffed)
>
> In addition to the other reply:
>
> >$_POST["guess"] = (int) $_POST["guess"];

>
> There's no element 'guess' in the $_POST array when the script runs for
> the first time, which causes a notice.
>
> Micha


I didn't notice an, er, notice, but I see your point anyway.

While I'm here, can you or someone else please tell me why this won't work:

<html>
<head>
<title>Listing 9.15 A file upload script</title>
</head>
<?php
if ( isset( $_POST["fupload"] ) ) {
// code not shown as it never executes anyway.
}
else // added by me to confirm test failed
print ("not set<BR>");
?>

<body>
<form enctype="multipart/form-data" action="<?php print
($_SERVER["PHP_SELF"])?>" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="51200">
<input type="file" name="fupload"><br>
<input type="submit" value="Send file!">
</form>
</body>
</html>

As you'll see, this is a simple form to upload a file. Whether I send a
file or not, the test for 'fupload' fails.


Reply With Quote
  #7 (permalink)  
Old 09-06-2004
Miffed
 
Posts: n/a
Default Re: newbie needs help with checking for posted data

"Miffed" <me@here.com> wrote in message
news:chimnf$2f2$1@newsg2.svr.pol.co.uk...
> "Michael Fesser" <netizen@gmx.net> wrote in message
> news:ljmpj0hud6ogtmlh60k5v0an3c7rrf71c4@4ax.com...
> > .oO(Miffed)
> >
> > In addition to the other reply:
> >
> > >$_POST["guess"] = (int) $_POST["guess"];

> >
> > There's no element 'guess' in the $_POST array when the script runs for
> > the first time, which causes a notice.
> >
> > Micha

>
> I didn't notice an, er, notice, but I see your point anyway.
>
> While I'm here, can you or someone else please tell me why this won't

work:
>
> <html>
> <head>
> <title>Listing 9.15 A file upload script</title>
> </head>
> <?php
> if ( isset( $_POST["fupload"] ) ) {
> // code not shown as it never executes anyway.
> }
> else // added by me to confirm test failed
> print ("not set<BR>");
> ?>
>
> <body>
> <form enctype="multipart/form-data" action="<?php print
> ($_SERVER["PHP_SELF"])?>" method="POST">
> <input type="hidden" name="MAX_FILE_SIZE" value="51200">
> <input type="file" name="fupload"><br>
> <input type="submit" value="Send file!">
> </form>
> </body>
> </html>
>
> As you'll see, this is a simple form to upload a file. Whether I send a
> file or not, the test for 'fupload' fails.


I've just worked out I have to use a $_FILES prefix, but now the test is
true even BEFORE I send a file!


Reply With Quote
  #8 (permalink)  
Old 09-06-2004
Michael Fesser
 
Posts: n/a
Default Re: newbie needs help with checking for posted data

.oO(Miffed)

>"Michael Fesser" <netizen@gmx.net> wrote
>>
>> >$_POST["guess"] = (int) $_POST["guess"];

>>
>> There's no element 'guess' in the $_POST array when the script runs for
>> the first time, which causes a notice.

>
>I didn't notice an, er, notice, but I see your point anyway.


Set error_reporting to E_ALL in your php.ini to see all warnings and
notices. Good code should be free of such things, so E_ALL is a must on
a development system.

>While I'm here, can you or someone else please tell me why this won't work:
>
>[...]
>if ( isset( $_POST["fupload"] ) ) {
>// code not shown as it never executes anyway.
>}
>else // added by me to confirm test failed
> print ("not set<BR>");
>?>
>[...]
> <input type="file" name="fupload"><br>
>[...]
>
>As you'll see, this is a simple form to upload a file. Whether I send a
>file or not, the test for 'fupload' fails.


Yep, because it's not stored in the $_POST array, but in $_FILES
instead. Do a

print '<pre>';
print_r($_FILES);
print '</pre>';

at the beginning of the script, then call it in the browser and upload
something.

Chapter 34. Handling file uploads
http://www.php.net/manual/en/features.file-upload.php

HTH
Micha
Reply With Quote
  #9 (permalink)  
Old 09-06-2004
Michael Fesser
 
Posts: n/a
Default Re: newbie needs help with checking for posted data

.oO(Miffed)

>> <form enctype="multipart/form-data" action="<?php print
>> ($_SERVER["PHP_SELF"])?>" method="POST">
>> <input type="hidden" name="MAX_FILE_SIZE" value="51200">
>> <input type="file" name="fupload"><br>
>> <input type="submit" value="Send file!">
>> </form>
>> </body>
>> </html>
>>
>> As you'll see, this is a simple form to upload a file. Whether I send a
>> file or not, the test for 'fupload' fails.

>
>I've just worked out I have to use a $_FILES prefix, but now the test is
>true even BEFORE I send a file!


You could test for $_POST['submit'] instead.

Micha
Reply With Quote
Reply


Thread Tools
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

vB 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:50 AM.


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