Best way to test for form submission?

Best way to test for form submission?

am 28.08.2009 05:09:29 von vendion

This question might give away the fact that I am a php noob, but I am
looking for the best way to test for form submission in PHP. I know in
Perl this can be done with

if (param)

but I don't know if that will work with PHP. I have read the Learning
PHP 5 book and the only thing that was mentioned in the book was the use
of something like this

print "

Hello ".$_POST['username']."

";

I'm sure that this is not the best/recommended way to do this but I'm
hoping someone here will point me in the right direction.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Best way to test for form submission?

am 28.08.2009 05:21:58 von vendion

On 08/27/2009 11:09 PM, Adam Jimerson wrote:
> This question might give away the fact that I am a php noob, but I am
> looking for the best way to test for form submission in PHP. I know in
> Perl this can be done with
>
> if (param)
>
> but I don't know if that will work with PHP. I have read the Learning
> PHP 5 book and the only thing that was mentioned in the book was the use
> of something like this
>
> print "

Hello ".$_POST['username']."

";

Sorry copied and pasted the wrong line (long day)

if (array_key_exists('username',$_POST))
>
> I'm sure that this is not the best/recommended way to do this but I'm
> hoping someone here will point me in the right direction.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Best way to test for form submission?

am 28.08.2009 15:50:19 von Ashley Sheridan

On Thu, 2009-08-27 at 23:21 -0400, Adam Jimerson wrote:
> On 08/27/2009 11:09 PM, Adam Jimerson wrote:
> > This question might give away the fact that I am a php noob, but I am
> > looking for the best way to test for form submission in PHP. I know in
> > Perl this can be done with
> >
> > if (param)
> >
> > but I don't know if that will work with PHP. I have read the Learning
> > PHP 5 book and the only thing that was mentioned in the book was the use
> > of something like this
> >
> > print "

Hello ".$_POST['username']."

";
>
> Sorry copied and pasted the wrong line (long day)
>
> if (array_key_exists('username',$_POST))
> >
> > I'm sure that this is not the best/recommended way to do this but I'm
> > hoping someone here will point me in the right direction.
>
>

The best way I've found is to do something like this:

if(isset($_POST['submit']))
{}

Note that in-place of submit you can put the name of any form element. I
chose submit here, because every form should have a submit button. Note
also that this will only work if you have given your submit button a
name:



Thanks,
Ash
http://www.ashleysheridan.co.uk




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Best way to test for form submission?

am 28.08.2009 15:50:40 von Nitsan Bin-Nun

--0016e65aef486a84ed047233f9d1
Content-Type: text/plain; charset=ISO-8859-1


if (isset($_POST['submitted'])) doThings();
else echo "

";

On Fri, Aug 28, 2009 at 5:21 AM, Adam Jimerson wrote:

> On 08/27/2009 11:09 PM, Adam Jimerson wrote:
> > This question might give away the fact that I am a php noob, but I am
> > looking for the best way to test for form submission in PHP. I know in
> > Perl this can be done with
> >
> > if (param)
> >
> > but I don't know if that will work with PHP. I have read the Learning
> > PHP 5 book and the only thing that was mentioned in the book was the use
> > of something like this
> >
> > print "

Hello ".$_POST['username']."

";
>
> Sorry copied and pasted the wrong line (long day)
>
> if (array_key_exists('username',$_POST))
> >
> > I'm sure that this is not the best/recommended way to do this but I'm
> > hoping someone here will point me in the right direction.
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--0016e65aef486a84ed047233f9d1--

Re: Re: Best way to test for form submission?

am 28.08.2009 15:53:56 von Stut

2009/8/28 Adam Jimerson :
> On 08/27/2009 11:09 PM, Adam Jimerson wrote:
>> This question might give away the fact that I am a php noob, but I am
>> looking for the best way to test for form submission in PHP.  I kno=
w in
>> Perl this can be done with
>>
>> if (param)
>>
>> but I don't know if that will work with PHP.  I have read the Learn=
ing
>> PHP 5 book and the only thing that was mentioned in the book was the use
>> of something like this
>>
>> print "

Hello ".$_POST['username']."

";
>
> Sorry copied and pasted the wrong line (long day)
>
> if (array_key_exists('username',$_POST))
>>
>> I'm sure that this is not the best/recommended way to do this but I'm
>> hoping someone here will point me in the right direction.

Two options...

1) if (isset($_POST['username'])) // Specific to any given form

2) if (count($_POST) > 0) // Just tests whether any data was POSTed

There are other ways to do it but I usually use one of these two.

-Stuart

--=20
http://stut.net/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Best way to test for form submission?

am 28.08.2009 15:55:38 von Eric Lee

DQotLS0tLSBPcmlnaW5hbCBNZXNzYWdlIC0tLS0tIA0KRnJvbTogIkFkYW0g SmltZXJzb24iIDx2
ZW5kaW9uQGNoYXJ0ZXIubmV0Pg0KVG86IDxwaHAtZ2VuZXJhbEBsaXN0cy5w aHAubmV0Pg0KU2Vu
dDogRnJpZGF5LCBBdWd1c3QgMjgsIDIwMDkgMTE6MjEgQU0NClN1YmplY3Q6 IFtQSFBdIFJlOiBC
ZXN0IHdheSB0byB0ZXN0IGZvciBmb3JtIHN1Ym1pc3Npb24/DQoNCg0KPiBP biAwOC8yNy8yMDA5
IDExOjA5IFBNLCBBZGFtIEppbWVyc29uIHdyb3RlOg0KPj4gVGhpcyBxdWVz dGlvbiBtaWdodCBn
aXZlIGF3YXkgdGhlIGZhY3QgdGhhdCBJIGFtIGEgcGhwIG5vb2IsIGJ1dCBJ IGFtDQo+PiBsb29r
aW5nIGZvciB0aGUgYmVzdCB3YXkgdG8gdGVzdCBmb3IgZm9ybSBzdWJtaXNz aW9uIGluIFBIUC4g
IEkga25vdyBpbg0KPj4gUGVybCB0aGlzIGNhbiBiZSBkb25lIHdpdGgNCj4+ IA0KPj4gaWYgKHBh
cmFtKQ0KPj4gDQo+PiBidXQgSSBkb24ndCBrbm93IGlmIHRoYXQgd2lsbCB3 b3JrIHdpdGggUEhQ
LiAgSSBoYXZlIHJlYWQgdGhlIExlYXJuaW5nDQo+PiBQSFAgNSBib29rIGFu ZCB0aGUgb25seSB0
aGluZyB0aGF0IHdhcyBtZW50aW9uZWQgaW4gdGhlIGJvb2sgd2FzIHRoZSB1 c2UNCj4+IG9mIHNv
bWV0aGluZyBsaWtlIHRoaXMNCj4+IA0KPj4gcHJpbnQgIjxwPkhlbGxvICIu JF9QT1NUWyd1c2Vy
bmFtZSddLiI8L3A+IjsNCj4gDQo+IFNvcnJ5IGNvcGllZCBhbmQgcGFzdGVk IHRoZSB3cm9uZyBs
aW5lIChsb25nIGRheSkNCj4gDQo+IGlmIChhcnJheV9rZXlfZXhpc3RzKCd1 c2VybmFtZScsJF9Q
T1NUKSkNCg0KSGVyZSBpcyBhbm90aGVyIHdheQ0KDQppZiAoaXNzZXQoJF9Q T1NUWyd1c2VybmFt
ZSddKSAgJiYgJF9QT1NUWyd1c2VybmFtZSddICE9ICcnKQ0KeyAuLi4NCg0K DQo+PiANCj4+IEkn
bSBzdXJlIHRoYXQgdGhpcyBpcyBub3QgdGhlIGJlc3QvcmVjb21tZW5kZWQg d2F5IHRvIGRvIHRo
aXMgYnV0IEknbQ0KPj4gaG9waW5nIHNvbWVvbmUgaGVyZSB3aWxsIHBvaW50 IG1lIGluIHRoZSBy
aWdodCBkaXJlY3Rpb24uDQo+IA0KPiANCj4gLS0gDQo+IFBIUCBHZW5lcmFs IE1haWxpbmcgTGlz
dCAoaHR0cDovL3d3dy5waHAubmV0LykNCj4gVG8gdW5zdWJzY3JpYmUsIHZp c2l0OiBodHRwOi8v
d3d3LnBocC5uZXQvdW5zdWIucGhwDQo+IA0KPg==

Re: Re: Best way to test for form submission?

am 28.08.2009 16:06:15 von Martin Scotta

--0016e648082c00c1d404723430f6
Content-Type: text/plain; charset=ISO-8859-1

what about a simple

if( count( $_POST ))

All you need is to check a form submission

On Fri, Aug 28, 2009 at 10:55 AM, Eric wrote:

>
> ----- Original Message -----
> From: "Adam Jimerson"
> To:
> Sent: Friday, August 28, 2009 11:21 AM
> Subject: [PHP] Re: Best way to test for form submission?
>
>
> > On 08/27/2009 11:09 PM, Adam Jimerson wrote:
> >> This question might give away the fact that I am a php noob, but I am
> >> looking for the best way to test for form submission in PHP. I know in
> >> Perl this can be done with
> >>
> >> if (param)
> >>
> >> but I don't know if that will work with PHP. I have read the Learning
> >> PHP 5 book and the only thing that was mentioned in the book was the use
> >> of something like this
> >>
> >> print "

Hello ".$_POST['username']."

";
> >
> > Sorry copied and pasted the wrong line (long day)
> >
> > if (array_key_exists('username',$_POST))
>
> Here is another way
>
> if (isset($_POST['username']) && $_POST['username'] != '')
> { ...
>
>
> >>
> >> I'm sure that this is not the best/recommended way to do this but I'm
> >> hoping someone here will point me in the right direction.
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>



--
Martin Scotta

--0016e648082c00c1d404723430f6--

Re: Re: Best way to test for form submission?

am 28.08.2009 16:10:23 von Stut

2009/8/28 Eric :
>
> ----- Original Message -----
> From: "Adam Jimerson"
> To:
> Sent: Friday, August 28, 2009 11:21 AM
> Subject: [PHP] Re: Best way to test for form submission?
>
>
>> On 08/27/2009 11:09 PM, Adam Jimerson wrote:
>>> This question might give away the fact that I am a php noob, but I am
>>> looking for the best way to test for form submission in PHP.  I kn=
ow in
>>> Perl this can be done with
>>>
>>> if (param)
>>>
>>> but I don't know if that will work with PHP.  I have read the Lear=
ning
>>> PHP 5 book and the only thing that was mentioned in the book was the us=
e
>>> of something like this
>>>
>>> print "

Hello ".$_POST['username']."

";
>>
>> Sorry copied and pasted the wrong line (long day)
>>
>> if (array_key_exists('username',$_POST))
>
> Here is another way
>
> if (isset($_POST['username'])  && $_POST['username'] !=3D '')
> { ...

If you use this method then you'll completely ignore a form submission
if they've forgotten to enter a username. Detection and validation are
two distinct steps and should be treated as such.

-Stuart

--=20
http://stut.net/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Best way to test for form submission?

am 28.08.2009 16:17:48 von Paul M Foster

On Fri, Aug 28, 2009 at 02:53:56PM +0100, Stuart wrote:

> 2009/8/28 Adam Jimerson :
> > On 08/27/2009 11:09 PM, Adam Jimerson wrote:
> >> This question might give away the fact that I am a php noob, but I a=
m
> >> looking for the best way to test for form submission in PHP. =A0I kn=
ow in
> >> Perl this can be done with
> >>
> >> if (param)
> >>
> >> but I don't know if that will work with PHP. =A0I have read the Lear=
ning
> >> PHP 5 book and the only thing that was mentioned in the book was the=
use
> >> of something like this
> >>
> >> print "

Hello ".$_POST['username']."

";
> >
> > Sorry copied and pasted the wrong line (long day)
> >
> > if (array_key_exists('username',$_POST))
> >>
> >> I'm sure that this is not the best/recommended way to do this but I'=
m
> >> hoping someone here will point me in the right direction.
>=20
> Two options...
>=20
> 1) if (isset($_POST['username'])) // Specific to any given form
>=20
> 2) if (count($_POST) > 0) // Just tests whether any data was POSTed

FWIW, the CodeIgniter framework uses this latter method internally.

Paul

--=20
Paul M. Foster

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Best way to test for form submission?

am 28.08.2009 17:03:05 von Stut

2009/8/28 Eric :
>
> ----- Original Message -----
> From: "Stuart"
> To: "Eric"
> Cc: ; "Adam Jimerson"
> Sent: Friday, August 28, 2009 10:10 PM
> Subject: Re: [PHP] Re: Best way to test for form submission?
>
>
> 2009/8/28 Eric :
>>
>> ----- Original Message -----
>> From: "Adam Jimerson"
>> To:
>> Sent: Friday, August 28, 2009 11:21 AM
>> Subject: [PHP] Re: Best way to test for form submission?
>>
>>
>>> On 08/27/2009 11:09 PM, Adam Jimerson wrote:
>>>> This question might give away the fact that I am a php noob, but I am
>>>> looking for the best way to test for form submission in PHP. I know in
>>>> Perl this can be done with
>>>>
>>>> if (param)
>>>>
>>>> but I don't know if that will work with PHP. I have read the Learning
>>>> PHP 5 book and the only thing that was mentioned in the book was the u=
se
>>>> of something like this
>>>>
>>>> print "

Hello ".$_POST['username']."

";
>>>
>>> Sorry copied and pasted the wrong line (long day)
>>>
>>> if (array_key_exists('username',$_POST))
>>
>> Here is another way
>>
>> if (isset($_POST['username']) && $_POST['username'] !=3D '')
>> { ...
>
> If you use this method then you'll completely ignore a form submission
> if they've forgotten to enter a username. Detection and validation are
> two distinct steps and should be treated as such.
>
> Stuart
> thanks,
>
> how about this way
>
> $submit =3D isset($_POST['submit']) ? true : false;
> if ($submit)
>  { ...

Why the variable? It's unnecessary, as is the ?: operator - isset
returns a boolean.

if (isset($_POST['submit'])) will achieve the same result.

-Stuart

--=20
http://stut.net/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Best way to test for form submission?

am 28.08.2009 17:29:35 von TedD

At 3:10 PM +0100 8/28/09, Stuart wrote:
>2009/8/28 Eric :
> > Here is another way
>>
>> if (isset($_POST['username']) && $_POST['username'] != '')
>> { ...
>
>If you use this method then you'll completely ignore a form submission
>if they've forgotten to enter a username. Detection and validation are
>two distinct steps and should be treated as such.
>
>-Stuart


I usually treat forms this way:

$action = isset($_POST['action']) ? $_POST['action'] : null;

and then deal with $action.

Dealing with $action means that I might clean/scrub it for storage in
MySQL, or use it to determine a course of processing, or any number
of other things.

The point is to check IF a $_POST has occurred, without triggering an
error if it hasn't, and to deal with the outcome either way.

Cheers,

tedd

--
-------
http://sperling.com http://ancientstones.com http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Best way to test for form submission?

am 28.08.2009 17:32:00 von Ashley Sheridan

On Fri, 2009-08-28 at 11:29 -0400, tedd wrote:
> At 3:10 PM +0100 8/28/09, Stuart wrote:
> >2009/8/28 Eric :
> > > Here is another way
> >>
> >> if (isset($_POST['username']) && $_POST['username'] != '')
> >> { ...
> >
> >If you use this method then you'll completely ignore a form submission
> >if they've forgotten to enter a username. Detection and validation are
> >two distinct steps and should be treated as such.
> >
> >-Stuart
>
>
> I usually treat forms this way:
>
> $action = isset($_POST['action']) ? $_POST['action'] : null;
>
> and then deal with $action.
>
> Dealing with $action means that I might clean/scrub it for storage in
> MySQL, or use it to determine a course of processing, or any number
> of other things.
>
> The point is to check IF a $_POST has occurred, without triggering an
> error if it hasn't, and to deal with the outcome either way.
>
> Cheers,
>
> tedd
>
> --
> -------
> http://sperling.com http://ancientstones.com http://earthstones.com
>

I have noticed a bug with using a form element named action, which
funnily enough only effects IE. If you need to do anything to or based
on that action element in IE with Javascript, it has a lot of problems
differentiating the form element from the form action attribute, and
gets them a bit confused. It's similar to the IE name/id bug.

Thanks,
Ash
http://www.ashleysheridan.co.uk




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Best way to test for form submission?

am 28.08.2009 17:39:48 von TedD

At 4:32 PM +0100 8/28/09, Ashley Sheridan wrote:
>
>
>I have noticed a bug with using a form element named action, which
>funnily enough only effects IE. If you need to do anything to or based
>on that action element in IE with Javascript, it has a lot of problems
>differentiating the form element from the form action attribute, and
>gets them a bit confused. It's similar to the IE name/id bug.
>
>Thanks,
>Ash

Good catch.

I actually never use the word $action -- that's just a placeholder.

Cheers,

tedd

--
-------
http://sperling.com http://ancientstones.com http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Best way to test for form submission?

am 28.08.2009 17:49:53 von Ashley Sheridan

On Fri, 2009-08-28 at 11:39 -0400, tedd wrote:
> At 4:32 PM +0100 8/28/09, Ashley Sheridan wrote:
> >
> >
> >I have noticed a bug with using a form element named action, which
> >funnily enough only effects IE. If you need to do anything to or based
> >on that action element in IE with Javascript, it has a lot of problems
> >differentiating the form element from the form action attribute, and
> >gets them a bit confused. It's similar to the IE name/id bug.
> >
> >Thanks,
> >Ash
>
> Good catch.
>
> I actually never use the word $action -- that's just a placeholder.
>
> Cheers,
>
> tedd
>
> --
> -------
> http://sperling.com http://ancientstones.com http://earthstones.com
>
I seem to always use the word! *smacks self on wrist*

When I first came across it, it confused the hell out of me! Always
running into these kinds of Javascript bugs in IE, it has a much
narrower field of 'safe' variable names, probably because of all the
dithering about it's done in the past.

Thanks,
Ash
http://www.ashleysheridan.co.uk




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Best way to test for form submission?

am 28.08.2009 18:28:30 von TedD

At 4:49 PM +0100 8/28/09, Ashley Sheridan wrote:
>
> >
>I seem to always use the word! *smacks self on wrist*
>
>When I first came across it, it confused the hell out of me! Always
>running into these kinds of Javascript bugs in IE, it has a much
>narrower field of 'safe' variable names, probably because of all the
>dithering about it's done in the past.
>
>Thanks,
>Ash

Ash:

What catches me every once in a while is using variable names that
are the same as $_SESSION indexes, such as:

$session_name = $_SESSION['session_name'];

Believe it or not, that does NOT always work! Sometimes I have to do this --

$my_session_name = $_SESSION['session_name'];

-- to get around the problem.

I have experienced this error more than once -- it's strange.

Cheers,

tedd

--
-------
http://sperling.com http://ancientstones.com http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Best way to test for form submission?

am 28.08.2009 18:33:36 von Ashley Sheridan

On Fri, 2009-08-28 at 12:28 -0400, tedd wrote:
> At 4:49 PM +0100 8/28/09, Ashley Sheridan wrote:
> >
> > >
> >I seem to always use the word! *smacks self on wrist*
> >
> >When I first came across it, it confused the hell out of me! Always
> >running into these kinds of Javascript bugs in IE, it has a much
> >narrower field of 'safe' variable names, probably because of all the
> >dithering about it's done in the past.
> >
> >Thanks,
> >Ash
>
> Ash:
>
> What catches me every once in a while is using variable names that
> are the same as $_SESSION indexes, such as:
>
> $session_name = $_SESSION['session_name'];
>
> Believe it or not, that does NOT always work! Sometimes I have to do this --
>
> $my_session_name = $_SESSION['session_name'];
>
> -- to get around the problem.
>
> I have experienced this error more than once -- it's strange.
>
> Cheers,
>
> tedd
>
Does that happen even with register globals off?

Thanks,
Ash
http://www.ashleysheridan.co.uk




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Best way to test for form submission?

am 28.08.2009 18:33:44 von Ben Dunlap

I was surprised when no one recommended this:

if ($_SERVER['REQUEST_METHOD'] == 'POST')

So now I'm wondering if there's a pitfall to this method that I'm not
aware of...

Thanks,

Ben

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Best way to test for form submission?

am 28.08.2009 18:51:38 von Ashley Sheridan

On Fri, 2009-08-28 at 09:33 -0700, Ben Dunlap wrote:
> I was surprised when no one recommended this:
>
> if ($_SERVER['REQUEST_METHOD'] == 'POST')
>
> So now I'm wondering if there's a pitfall to this method that I'm not
> aware of...
>
> Thanks,
>
> Ben
>

Well, as far as I'm aware $_SERVER isn't reliable from server to server.
That said, I've never had a problem using it.

I probably wouldn't use this however, as it does only check for the
existence of POST data. Sometimes I'll trigger the same PHP script from
both POST and GET. For example, a search form would use POST in the
initial form, and then if it was paginated, the links forwards and
backwards through the results would be GET (like Google) I usually just
tend to use the $_REQUEST array instead of $_POST or $_GET. You get the
benefit of being able to work with both arrays (as well as $_SESSION and
$_COOKIE) without any drawbacks.

Thanks,
Ash
http://www.ashleysheridan.co.uk




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Best way to test for form submission?

am 28.08.2009 18:56:07 von Shawn McKenzie

Adam Jimerson wrote:
> This question might give away the fact that I am a php noob, but I am
> looking for the best way to test for form submission in PHP. I know in
> Perl this can be done with
>
> if (param)
>
> but I don't know if that will work with PHP. I have read the Learning
> PHP 5 book and the only thing that was mentioned in the book was the use
> of something like this
>
> print "

Hello ".$_POST['username']."

";
>
> I'm sure that this is not the best/recommended way to do this but I'm
> hoping someone here will point me in the right direction.

Just to throw it into the mix:

if(!empty($_POST)) for a general test of whether any form was posted.

--
Thanks!
-Shawn
http://www.spidean.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Best way to test for form submission?

am 28.08.2009 18:59:57 von Ben Dunlap

> Well, as far as I'm aware $_SERVER isn't reliable from server to server.
> That said, I've never had a problem using it.

Thanks -- I just looked it up and the manual says: "There is no
guarantee that every web server will provide any of these; servers may
omit some, or provide others not listed here. That said, a large
number of these variables are accounted for in the =BB CGI 1.1
specification, so you should be able to expect those."

So I guess it wouldn't make sense to rely on anything in $_SERVER if
you're building an app for widespread use; e.g., CodeIgniter, as
mentioned above.

> tend to use the $_REQUEST array instead of $_POST or $_GET. You get the
> benefit of being able to work with both arrays (as well as $_SESSION and
> $_COOKIE) without any drawbacks.

For now I'm inclined against $_REQUEST, since it's not yet supported
by filter_input(). I think filter_input() is the bee's knees and I've
stopped touching $_POST or $_GET directly since I discovered it.

Ben

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Best way to test for form submission?

am 28.08.2009 19:20:57 von Ashley Sheridan

On Sat, 2009-08-29 at 01:09 +0800, Eric wrote:
> ----- Original Message -----
> From: "Ashley Sheridan"
> To: "Ben Dunlap"
> Cc:
> Sent: Saturday, August 29, 2009 12:51 AM
> Subject: Re: [PHP] Re: Best way to test for form submission?
>
>
> > On Fri, 2009-08-28 at 09:33 -0700, Ben Dunlap wrote:
> >> I was surprised when no one recommended this:
> >>
> >> if ($_SERVER['REQUEST_METHOD'] == 'POST')
> >>
> >> So now I'm wondering if there's a pitfall to this method that I'm not
> >> aware of...
> >>
> >> Thanks,
> >>
> >> Ben
> >>
> >
> > Well, as far as I'm aware $_SERVER isn't reliable from server to server.
> > That said, I've never had a problem using it.
> >
> > I probably wouldn't use this however, as it does only check for the
> > existence of POST data. Sometimes I'll trigger the same PHP script from
> > both POST and GET. For example, a search form would use POST in the
> > initial form, and then if it was paginated, the links forwards and
> > backwards through the results would be GET (like Google) I usually just
> > tend to use the $_REQUEST array instead of $_POST or $_GET. You get the
> > benefit of being able to work with both arrays (as well as $_SESSION and
> > $_COOKIE) without any drawbacks.
>
>
> If only use the $_REQUEST global array. It may or may not be take a risk of
> CSRF. Take a look this linke http://www.cgisecurity.com/csrf-faq.html
>
> I read it a few days before.
>
> Eric
>
> >
> > Thanks,
> > Ash
> > http://www.ashleysheridan.co.uk
> >
> >
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
It's no less safe than just using $_POST (which the article itself
says). The sensible thing to do is mistrust ANY data coming from the
client machine, and always validate it before using it.

Thanks,
Ash
http://www.ashleysheridan.co.uk




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Best way to test for form submission?

am 28.08.2009 19:29:12 von Martin Scotta

--00c09f971e8ccf172a04723705e3
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

On Fri, Aug 28, 2009 at 1:59 PM, Ben Dunlap wro=
te:

> > Well, as far as I'm aware $_SERVER isn't reliable from server to server=
..
> > That said, I've never had a problem using it.
>
> Thanks -- I just looked it up and the manual says: "There is no
> guarantee that every web server will provide any of these; servers may
> omit some, or provide others not listed here. That said, a large
> number of these variables are accounted for in the =BB CGI 1.1
> specification, so you should be able to expect those."
>
> So I guess it wouldn't make sense to rely on anything in $_SERVER if
> you're building an app for widespread use; e.g., CodeIgniter, as
> mentioned above.
>
> > tend to use the $_REQUEST array instead of $_POST or $_GET. You get the
> > benefit of being able to work with both arrays (as well as $_SESSION an=
d
> > $_COOKIE) without any drawbacks.
>
> For now I'm inclined against $_REQUEST, since it's not yet supported
> by filter_input(). I think filter_input() is the bee's knees and I've
> stopped touching $_POST or $_GET directly since I discovered it.
>
> Ben
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
When the request is PUT your $_SERVER['REQUEST_METHOD'] detection will fail=
,
but the $_POST will be populated.

I think the best way is by count( $_POST ) > 0 but this only applies for
application/x-www-form-urlencoded forms.
Other content-type will not populate the $_POST and therefore need to be
validated by other method.

PHP provides non-yet-standard HttpRequest class that can handle this
perfectly


--=20
Martin Scotta

--00c09f971e8ccf172a04723705e3--

Re: Re: Best way to test for form submission?

am 28.08.2009 21:03:36 von J DeBord

--001636025dae8ad62f047238581f
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

If you want to check that a *form* was submiited, you need to at minimum
check that one of the form elements exist in the post data, assuming the
method is post. The submit button is a good one to use. Just checking that
there *is* POST data isn't a great idea. Anyone can post anything. Checking
for a key named 'submit' in the POST array is no more difficult than any
other method. That being said anyone could POST a key value pair with the
key being 'submit'. So validate everything!

On Fri, Aug 28, 2009 at 7:29 PM, Martin Scotta wrot=
e:

> On Fri, Aug 28, 2009 at 1:59 PM, Ben Dunlap > >wrote:
>
> > > Well, as far as I'm aware $_SERVER isn't reliable from server to
> server.
> > > That said, I've never had a problem using it.
> >
> > Thanks -- I just looked it up and the manual says: "There is no
> > guarantee that every web server will provide any of these; servers may
> > omit some, or provide others not listed here. That said, a large
> > number of these variables are accounted for in the » CGI 1.1
> > specification, so you should be able to expect those."
> >
> > So I guess it wouldn't make sense to rely on anything in $_SERVER if
> > you're building an app for widespread use; e.g., CodeIgniter, as
> > mentioned above.
> >
> > > tend to use the $_REQUEST array instead of $_POST or $_GET. You get t=
he
> > > benefit of being able to work with both arrays (as well as $_SESSION
> and
> > > $_COOKIE) without any drawbacks.
> >
> > For now I'm inclined against $_REQUEST, since it's not yet supported
> > by filter_input(). I think filter_input() is the bee's knees and I've
> > stopped touching $_POST or $_GET directly since I discovered it.
> >
> > Ben
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
> When the request is PUT your $_SERVER['REQUEST_METHOD'] detection will
> fail,
> but the $_POST will be populated.
>
> I think the best way is by count( $_POST ) > 0 but this only applies for
> application/x-www-form-urlencoded forms.
> Other content-type will not populate the $_POST and therefore need to be
> validated by other method.
>
> PHP provides non-yet-standard HttpRequest class that can handle this
> perfectly
>
>
> --
> Martin Scotta
>

--001636025dae8ad62f047238581f--

Re: Re: Best way to test for form submission?

am 29.08.2009 06:50:41 von keith

I've encountered issue with checking $_POST['submit']
if(isset($_POST['submit'])) {}

If the form consists of checkbox/radio and text field, some of my forms can
be submitted by just press [ENTER] at the end of one of the text field. In
this case, the $_POST['submit'] is set even the submit button was not
clicked.
However, in some of my forms, $_POST['submit'] will not be set if I submit
the form by pressing [ENTER] in one of the text field.
So, if the later case happen, I need to remind the user to explicitly click
the [Submit] button.
I don't know why or in what condition that pressing [ENTER] will not submit
the whole form include the $_POST['submit'].

Keith


"Ashley Sheridan" wrote in message
news:1251467419.27899.106.camel@localhost...
> On Thu, 2009-08-27 at 23:21 -0400, Adam Jimerson wrote:
>> On 08/27/2009 11:09 PM, Adam Jimerson wrote:
>> > This question might give away the fact that I am a php noob, but I am
>> > looking for the best way to test for form submission in PHP. I know in
>> > Perl this can be done with
>> >
>> > if (param)
>> >
>> > but I don't know if that will work with PHP. I have read the Learning
>> > PHP 5 book and the only thing that was mentioned in the book was the
>> > use
>> > of something like this
>> >
>> > print "

Hello ".$_POST['username']."

";
>>
>> Sorry copied and pasted the wrong line (long day)
>>
>> if (array_key_exists('username',$_POST))
>> >
>> > I'm sure that this is not the best/recommended way to do this but I'm
>> > hoping someone here will point me in the right direction.
>>
>>
>
> The best way I've found is to do something like this:
>
> if(isset($_POST['submit']))
> {}
>
> Note that in-place of submit you can put the name of any form element. I
> chose submit here, because every form should have a submit button. Note
> also that this will only work if you have given your submit button a
> name:
>
>
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Best way to test for form submission?

am 29.08.2009 07:59:02 von J DeBord

--000d615091548e493d047241804a
Content-Type: text/plain; charset=UTF-8

I've heard stories like this before, but never encountered it myself.

I forgot to mention that it is a good idea to give your submit buttons a
"value" attribute. Regardless of how the form is submitted, you should then
have a value for ['submit'].

Reminding the user that they must do anything other than the simplest things
is usually not a good approach. There will inevitably be users who do not
see/follow your reminder and use the enter button. Give the submit button a
value.


On Sat, Aug 29, 2009 at 6:50 AM, Keith wrote:

> I've encountered issue with checking $_POST['submit']
> if(isset($_POST['submit'])) {}
>
> If the form consists of checkbox/radio and text field, some of my forms can
> be submitted by just press [ENTER] at the end of one of the text field. In
> this case, the $_POST['submit'] is set even the submit button was not
> clicked.
> However, in some of my forms, $_POST['submit'] will not be set if I submit
> the form by pressing [ENTER] in one of the text field.
> So, if the later case happen, I need to remind the user to explicitly click
> the [Submit] button.
> I don't know why or in what condition that pressing [ENTER] will not submit
> the whole form include the $_POST['submit'].
>
> Keith
>
>
> "Ashley Sheridan" wrote in message
> news:1251467419.27899.106.camel@localhost...
>
> On Thu, 2009-08-27 at 23:21 -0400, Adam Jimerson wrote:
>>
>>> On 08/27/2009 11:09 PM, Adam Jimerson wrote:
>>> > This question might give away the fact that I am a php noob, but I am
>>> > looking for the best way to test for form submission in PHP. I know in
>>> > Perl this can be done with
>>> >
>>> > if (param)
>>> >
>>> > but I don't know if that will work with PHP. I have read the Learning
>>> > PHP 5 book and the only thing that was mentioned in the book was the >
>>> use
>>> > of something like this
>>> >
>>> > print "

Hello ".$_POST['username']."

";
>>>
>>> Sorry copied and pasted the wrong line (long day)
>>>
>>> if (array_key_exists('username',$_POST))
>>> >
>>> > I'm sure that this is not the best/recommended way to do this but I'm
>>> > hoping someone here will point me in the right direction.
>>>
>>>
>>>
>> The best way I've found is to do something like this:
>>
>> if(isset($_POST['submit']))
>> {}
>>
>> Note that in-place of submit you can put the name of any form element. I
>> chose submit here, because every form should have a submit button. Note
>> also that this will only work if you have given your submit button a
>> name:
>>
>>
>>
>> Thanks,
>> Ash
>> http://www.ashleysheridan.co.uk
>>
>>
>>
>>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--000d615091548e493d047241804a--

Re: Re: Best way to test for form submission?

am 29.08.2009 09:47:23 von news.NOSPAM.0ixbtqKe

On Sat, 29 Aug 2009 12:50:41 +0800, "Keith" wrote:

> I don't know why or in what condition that pressing [ENTER] will not submit
> the whole form include the $_POST['submit'].





/Nisse

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Re: Best way to test for form submission?

am 29.08.2009 10:18:47 von Warren Vail

To test a form I usually send the form contents to a php file that contains
the following;

foreach($_POST as $nm => $val) echo "_POST[".$nm."] [".$val."]
";
foreach($_GET as $nm => $val) echo "_GET[".$nm."] [".$val."]
";

Checkboxes and radio buttons only send their value if the control is
"checked".

You can have multiple submit buttons (type="submit") on a form, but you
should assign them different name parameters to recognize which one is
clicked (any one of them will cause the form to be submitted, but the only
one that will establish a $_POST entry named "submit" is the submit control
that is named "submit" (name="submit").

Pressing enter is not sensed by most controls.
Most browsers will look at the field the cursor is positioned on when enter
is pressed, and if it is not one of the controls that response to an enter,
it scans the DOM (list of controls), and will apply the enter to the first
control that responds to an enter, most common is the type="submit" control.


If a select control is between the text box and the submit button (in the
DOM list) the current entry for the select list will be selected and the
form will not be submitted unless the list includes a
onChange="this.form.submit(); " javascript entry.

Be careful designing forms that are dependent on some of these behaviors,
like positioning a submit button to the right of a text box to intentionally
receive the "enter", because different browsers will probably behave
differently.

Hope this helps a bit.

Warren Vail
Vail Systems Technology

-----Original Message-----
From: Keith [mailto:survivor_bus@hotmail.com]
Sent: Friday, August 28, 2009 9:51 PM
To: php-general@lists.php.net
Subject: Re: [PHP] Re: Best way to test for form submission?

I've encountered issue with checking $_POST['submit']
if(isset($_POST['submit'])) {}

If the form consists of checkbox/radio and text field, some of my forms can
be submitted by just press [ENTER] at the end of one of the text field. In
this case, the $_POST['submit'] is set even the submit button was not
clicked.
However, in some of my forms, $_POST['submit'] will not be set if I submit
the form by pressing [ENTER] in one of the text field.
So, if the later case happen, I need to remind the user to explicitly click
the [Submit] button.
I don't know why or in what condition that pressing [ENTER] will not submit
the whole form include the $_POST['submit'].

Keith


"Ashley Sheridan" wrote in message
news:1251467419.27899.106.camel@localhost...
> On Thu, 2009-08-27 at 23:21 -0400, Adam Jimerson wrote:
>> On 08/27/2009 11:09 PM, Adam Jimerson wrote:
>> > This question might give away the fact that I am a php noob, but I am
>> > looking for the best way to test for form submission in PHP. I know in
>> > Perl this can be done with
>> >
>> > if (param)
>> >
>> > but I don't know if that will work with PHP. I have read the Learning
>> > PHP 5 book and the only thing that was mentioned in the book was the
>> > use
>> > of something like this
>> >
>> > print "

Hello ".$_POST['username']."

";
>>
>> Sorry copied and pasted the wrong line (long day)
>>
>> if (array_key_exists('username',$_POST))
>> >
>> > I'm sure that this is not the best/recommended way to do this but I'm
>> > hoping someone here will point me in the right direction.
>>
>>
>
> The best way I've found is to do something like this:
>
> if(isset($_POST['submit']))
> {}
>
> Note that in-place of submit you can put the name of any form element. I
> chose submit here, because every form should have a submit button. Note
> also that this will only work if you have given your submit button a
> name:
>
>
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>

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

Re: Best way to test for form submission?

am 29.08.2009 10:47:09 von olavell

Shawn McKenzie wrote:
> Adam Jimerson wrote:
>> This question might give away the fact that I am a php noob, but I am
>> looking for the best way to test for form submission in PHP.

[..]

> Just to throw it into the mix:
>
> if(!empty($_POST)) for a general test of whether any form was posted.

There are more methods, I always use:

if($_SERVER["REQUEST_METHOD"] == "POST") {

do_something();

}


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Best way to test for form submission?

am 29.08.2009 15:05:50 von TedD

At 5:33 PM +0100 8/28/09, Ashley Sheridan wrote:
>On Fri, 2009-08-28 at 12:28 -0400, tedd wrote:
> > Ash:
>>
>> What catches me every once in a while is using variable names that
>> are the same as $_SESSION indexes, such as:
>>
>> $session_name = $_SESSION['session_name'];
>>
>> Believe it or not, that does NOT always work! Sometimes I have to do this --
>>
>> $my_session_name = $_SESSION['session_name'];
>>
>> -- to get around the problem.
>>
>> I have experienced this error more than once -- it's strange.
>>
>> Cheers,
>>
>> tedd
>>
>Does that happen even with register globals off?

Yes. it happens with register globals set to off

Cheers,

tedd

--
-------
http://sperling.com http://ancientstones.com http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Best way to test for form submission?

am 29.08.2009 15:17:38 von TedD

At 5:51 PM +0100 8/28/09, Ashley Sheridan wrote:
> I usually just
>tend to use the $_REQUEST array instead of $_POST or $_GET. You get the
>benefit of being able to work with both arrays (as well as $_SESSION and
>$_COOKIE) without any drawbacks.
>
>Thanks,
>Ash

Ash:

Drawbacks are funny things.

Not knowing where my data originated ($_GET, $_POST, or $_COOKIE) and
having the possibility of what I was expecting overridden in a
$_REQUEST is what I would call a drawback.

Cheers,

tedd

--
-------
http://sperling.com http://ancientstones.com http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Re: Best way to test for form submission?

am 29.08.2009 15:29:56 von TedD

At 1:18 AM -0700 8/29/09, Warren Vail wrote:
>To test a form I usually send the form contents to a php file that contains
>the following;
>
>foreach($_POST as $nm => $val) echo "_POST[".$nm."] [".$val."]
";
>foreach($_GET as $nm => $val) echo "_GET[".$nm."] [".$val."]
";
>
>Checkboxes and radio buttons only send their value if the control is
>"checked".
>

That's correct, here's the way I solve both types:

http://php1.net/b/form-radio
http://php1.net/b/form-radio1
http://php1.net/b/form-checkbox/
http://php1.net/b/form-checkbox1/

Cheers,

tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Best way to test for form submission?

am 29.08.2009 18:22:46 von Robert Cummings

Warren Vail wrote:
> To test a form I usually send the form contents to a php file that contains
> the following;
>
> foreach($_POST as $nm => $val) echo "_POST[".$nm."] [".$val."]
";
> foreach($_GET as $nm => $val) echo "_GET[".$nm."] [".$val."]
";
>
> Checkboxes and radio buttons only send their value if the control is
> "checked".
>
> You can have multiple submit buttons (type="submit") on a form, but you
> should assign them different name parameters to recognize which one is
> clicked (any one of them will cause the form to be submitted, but the only
> one that will establish a $_POST entry named "submit" is the submit control
> that is named "submit" (name="submit").

I would suggest NOT naming any field submit. There will come a time when
you will want to do form.submit() in JavaScript and you will find it
broken in one of the browsers. I'm not sure which, but one of them
breaks if you have named a field "submit". As a result I always use
"continue" instead :)

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Best way to test for form submission?

am 29.08.2009 19:59:58 von Tom Worster

On 8/29/09 9:29 AM, "tedd" wrote:

> At 1:18 AM -0700 8/29/09, Warren Vail wrote:
>> To test a form I usually send the form contents to a php file that contains
>> the following;
>>
>> foreach($_POST as $nm => $val) echo "_POST[".$nm."] [".$val."]
";
>> foreach($_GET as $nm => $val) echo "_GET[".$nm."] [".$val."]
";
>>
>> Checkboxes and radio buttons only send their value if the control is
>> "checked".
>>
>
> That's correct, here's the way I solve both types:
>
> http://php1.net/b/form-radio
> http://php1.net/b/form-radio1
> http://php1.net/b/form-checkbox/
> http://php1.net/b/form-checkbox1/

warren's test script above doesn't work so well with tedd's scheme for
naming radios & checkboxs. tedd uses name="option[]" in the markup so in
warren's script, when $nm is 'option', $val will be an array so it won't
convert to a string in ".$val.".



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Best way to test for form submission?

am 01.09.2009 03:32:39 von Ross McKay

On Sat, 29 Aug 2009 04:47:09 -0400, "O. Lavell" wrote:

>There are more methods, I always use:
>
>if($_SERVER["REQUEST_METHOD"] == "POST") {
>
> do_something();
>
>}

+1. Although, this doesn't catch PUT requests, but I have yet to
encounter a place where I want one with form data.
--
Ross McKay, Toronto, NSW Australia
"Let the laddie play wi the knife - he'll learn"
- The Wee Book of Calvin

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php