...

View Full Version : Interesting problem w/PHP and pressing return key in a form



bcarl314
01-21-2004, 02:01 PM
I've got a form with a few text elements, a reset button, and a submit button.

I'm grabbing the posted form data using $_POST['myTextElement'] and I noticed something unusual. If the cursor is in the text field and the user hits thier "Enter" key, the form is submitted, but no post data is available. If they click on the submit button, or manually tab until the submit button is in focus, then the data comes through all right. Am I doing something wrong?

Thanks

<edit>
Browser = MSIE 6.0 on WinXP Pro = Standard Corp Browser
</edit>

stuartc1
01-21-2004, 02:23 PM
please post your form so i can have a look...

stuartc1

bcarl314
01-21-2004, 03:42 PM
<form action="signUp.php" method="post">
<fieldset>
<legend>Metric enrollment form</legend>
<table border="0" summary="This table contains a form for registering for email notifications">
<tr>
<td>
<label for="email">Please enter your email address:</label>
<br /><span class="small">Email address must end in
<br />&quot;server.com&quot;.</span>
</td>
<td valign="top">
<input type="text" size="50" id="email" name="email" />
</td>
</tr>
<tr>
<td>
<input type="reset" value="Clear form" />
</td>
<td>
<input type="submit" value="Submit" name="submit" />
</td>
</tr>
</table>
</fieldset>
</form>

mordred
01-21-2004, 04:54 PM
IE does not send the key-value pair of the submit button if the return key is pressed in a text field. That means it could appear in the backend script as if no POST data was sent if you check for the existence of the submit value, in order to determine whether a POST request has been made or not.

In other words, this will not work reliably in IE:



if (isset($_POST['submit']) && $_POST['submit'] == 'Submit') {
var_dump($_POST);
}


but this will:



if (count($_POST) > 0) {
var_dump($_POST);
}


That's of course only useful to you if you have such an if clause somewhere in your backend script; if you don't, post the processing script's code.

bcarl314
01-21-2004, 04:56 PM
Aha, I did not know that. Thanks mordred. I fixed one line and all works fine.

raf
01-21-2004, 11:25 PM
IE does not send the key-value pair of the submit button if the return key is pressed in a text field. That means it could appear in the backend script as if no POST data was sent if you check for the existence of the submit value, in order to determine whether a POST request has been made or not.

I didn't know that either.

I'm not sure it is correct. I've tried it on a form of mine, and the
if (isset($_POST['submit']) && $_POST['submit'] == 'Submit') {
still returns true if i enter while my cursor is in a textfield. (IE 6 on Win 2K)
Well the submitbutton in named 'action' if that would matter.

Still the

if (count($_POST) > 0) {
var_dump($_POST);
}

is a nice tip, because its truly universal and will also work if you use javascript for your submit (for instance if you want an image as submitbutton or have an onchange="submit();" ) (---> the more univeral code you know, the less you need to remember)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum