Solved

Form Submitted via OnKeyPress when Enter Key Pressed

Posted on 2007-11-20
5
13,276 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Popup window in I.E. 8 62
Flash movie autostarts and web pages 2 57
cookies analysis tools 2 100
Enforce Chrome to open certain links in IE 6 37
I recently found myself in a Corporate Situation where the client had requested blocking access to any and all websites except his own Domain? Easy? I am sure this would be your answer but their requirement was, this has to be done without using…
#Citrix #Internet Explorer #Enterprise Mode #IE 11 #IE 8
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…
How to create a custom search shortcut to site-search Experts Exchange using Google in the Firefox browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch your Bookmark Menu: Press 'Ctrl +…

840 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