Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Form Submitted via OnKeyPress when Enter Key Pressed

Posted on 2007-11-20
5
Medium Priority
?
13,336 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 1000 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

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

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

Bada platform is becoming more and more famous this days and people talking about same. Some friends included those who have bada OS mobile asked me "what is bada?"and "what its features?". That encouraged me to research and write this article. [st…
When I'm searching for answers on Experts Exchange, I often use Google because it's built into my browser.  To search only on Experts Exchange, I use the "site:" search operator, which can be cumbersome to type out each time I want to run a search.…
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.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

578 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