Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts

    Interesting problem w/PHP and pressing return key in a form

    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>

  • #2
    New Coder
    Join Date
    Jan 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    please post your form so i can have a look...

    stuartc1
    -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
    www.php-editors.com - PHP Editors Reviewed
    www.phphub.com - PHP Community Hub
    www.php-freelancers.com - PHP Freelance Jobs
    -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~

  • #3
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Code:
    <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>

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

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

    but this will:

    PHP Code:
    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.
    De gustibus non est disputandum.

  • #5
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Aha, I did not know that. Thanks mordred. I fixed one line and all works fine.

  • #6
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •