Page 1 of 1

Form - clicking submit vs. hitting enter

Posted: Wed Sep 26, 2007 6:19 am
by squibs
I have a php form as follows:

Code: Select all

<form style="width: 345px" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="domain" name="domainform">
with some form elements

and a submit button:

Code: Select all

<input class="text" type="submit" name="submitBtn" value="Check domain"/>
I have php code to do some work on the submit:

Code: Select all

if (isset($_POST['submitBtn'])){
If I click submit, all is well, but if I hit the return key with a form element in focus, the page just reloads. Putting an else statement on the if above, the "if" is executed on clicking submit and the "else" is triggered when I press the return key.

So what is happening when I hit the return key with a form element in focus, and what other condition can I check to get the code in the if statement to run?

Thanks.

Posted: Wed Sep 26, 2007 7:18 am
by superdezign
Firstly, be careful with PHP_SELF, as PHP_SELF can open you up to XSS.

Secondly, the problem that your are describing is one that we discovered to happen in Internet Explorer, so we don't check against the submit button anymore. You could use a hidden element and check against it, or simply use:

Code: Select all

if (!empty($_POST)) {

Posted: Wed Sep 26, 2007 7:33 am
by squibs
Thanks - that worked just fine. Point taken about php_self - that was a good article.

So does anybody know what is going on when you hit return in a form in IE? Does it miss some of the POST vars?

Posted: Wed Sep 26, 2007 9:48 am
by feyd
IE doesn't use the submit button unless you have it focused.


Check the request method instead of $_POST.

$_SERVER['REQUEST_METHOD'].