Solved

Form Submitted via OnKeyPress when Enter Key Pressed

Posted on 2007-11-20
5
13,268 Views
Last Modified: 2013-12-08
I have a form that I want to submit when the user presses Enter. The action URL is determined when the form is submitted based on what the user has selected from a dropdown. In IE, the form is being submitting twice. This is causing a problem on the server side. In Firefox, the form is submitted only once and everything works as expected. How can I code this so IE will only submit the form once? Code below.

<INPUT type="text" name="CONTRACT_ID" onkeypress="javascript:submitOnEnter(event)">

<SCRIPT>
    function submitOnEnter(e) {
            var ENTER_KEY = 13;
            var code = "";
      
            if (window.event) // IE
            {
                code = e.keyCode;
            }
            else if (e.which) // Netscape/Firefox/Opera
            {
                code = e.which;
            }
            
            if (code == ENTER_KEY) {
                doSubmit();
                return false;
            }
      }

  function doSubmit() {
      // omitted logic that sets the action on the form based on the selected option
      document.frmSearch.submit();
  }

</SCRIPT>
0
Comment
Question by:craigdawson
  • 4
5 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
craigdawson,

Are you not using normal form tags or something besides a submit button?  Is there more than one form or submit button?

The behavior you want (pressing Enter submits form) is the default behavior for a form.  The reason it is occurring twice is probably this default behavior.  What happens if you don't use the onkeypress Event?

Let me know if you have any questions or need more information.

b0lsc0tt
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
craigdawson,

If you want to continue to use the event for whatever reason then try to fix this with ...

<INPUT type="text" name="CONTRACT_ID" onkeypress="javascript:submitOnEnter(event); return false;">

Adding "return false" to the event should stop any further default behavior by the object.  In theory that should fix the issue. :)

Let me know if you have a question.

b0lsc0tt
0
 

Author Comment

by:craigdawson
Comment Utility
Without the OnKeyPress event the form is not submitted in <enter> because there is no action defined for the form. The action URL  is determined on submit.

Adding "return false" does prevent the double-post. I had tried that. But the other consequence of this is that keystrokes are ignored in the input box! Can't type in it, can only paste. Obviously that's no good.
0
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 250 total points
Comment Utility
You can get the character to appear and use the return false by changing onkeypress to onkeyup.  That event should be as supported as onkeypress and so it is a valid way to fix this.

Another thing to try (worked in my test) is to add 'onsubmit="return false;"' to the form tag (no single quotes).  That seems to stop the second submit.  You would not need the return false in the onkeypress event.

I still am not clear on why you need the submit() method call in the script but that probably doesn't matter. :)  Either of these options should fix this for you.

If not then please provide the html for the form and any other script that is part of this process.  Let me know if you have a question.

bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
Several part series to implement Internet Explorer 11 Enterprise Mode
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now