Solved

Form Submitted via OnKeyPress when Enter Key Pressed

Posted on 2007-11-20
5
13,270 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
ID: 20323884
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
ID: 20323894
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
ID: 20327716
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
ID: 20336047
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
ID: 20340914
I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I spend far too much time on the web keeping up with the news: politics, the environment, computer stuff, the Experts Exchange. It's never-ending. But many of the most informative web pages are overwhelmed with noise: scrolling banners, flashing tex…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.
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…

939 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

4 Experts available now in Live!

Get 1:1 Help Now