Form - clicking submit vs. hitting enter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
squibs
Forum Newbie
Posts: 3
Joined: Tue Apr 10, 2007 5:21 am

Form - clicking submit vs. hitting enter

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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)) {
squibs
Forum Newbie
Posts: 3
Joined: Tue Apr 10, 2007 5:21 am

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

IE doesn't use the submit button unless you have it focused.


Check the request method instead of $_POST.

$_SERVER['REQUEST_METHOD'].
Post Reply