Announcement Announcement Module
Collapse
No announcement yet.
onSubmit executed twice through javascript Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • onSubmit executed twice through javascript

    I have some javascript setup that gets my search form by id and executes the .submit() action - the problem is that among other events, I had this JavaScript code listening for the Enter key press but what was happening was that the SimpleFormController onSubmit() method was (randomly?) getting executed twice

    I've commented out the listening for the Enter key press and interestingly enough the Enter key still submits the form - however, for some reason the Enter key doesn't work the first time the page is loaded (which is possibly where the randomness comes into play)

    so my question is, how can I still use the JavaScript to submit the form, but have a check that makes sure onSubmit() isn't already running?

  • #2
    To be honest...

    I would fix that JavaScript

    If you want to do a dodgy hack, you can always use a parameter stored in the session. Set it to true on method arrival and set it to false when the thread leaves the method. Use an if-statement if(executing != true){ // do stuff}else{return;}.

    No honestly, I would never do that. Fix the JavaScript!

    G

    Comment


    • #3
      well theres my problem, the JavaScript code itself is ok, its the fact that the form submits on Enter without it (most of the time)

      for example, here is the javascript:
      Code:
      function doOnEnter(event, action) {
          if (event && event.keyCode && event.keyCode == 13 && typeof action == "function") {
              action();
          }
      }
      
      function submitSearch() {
          var searchForm = byId("search_form");
          //.... other random checks...
          if (searchForm) {
          	searchForm.submit();
          }
      }
      and the form textbox that calls on Enter:
      Code:
      <input type="text" name="${status.expression}" value="${status.value}"
      					id="${status.expression}"  onkeydown="doOnEnter(event,submitSearch);"/>
      I threw some alert(); popups all over the javascript and it would only get called once even though the logging of the Controller shows the onSubmit running twice - I've since commented out action(); and the duplicate onSubmit has stopped but the Enter listen doesn't work when the page first loads

      Comment


      • #4
        If you press return in a field it will automatically submit the form. At the same time you submit it via JavaScript, hence submitted twice.

        Change to:

        Code:
        <input type="text" name="${status.expression}" value="${status.value}" id="${status.expression}"  onkeydown="return doOnEnter(event,submitSearch);"/>
        and

        Code:
        function doOnEnter(event, action) {
            if (event && event.keyCode && event.keyCode == 13 && typeof action == "function") {
                return action();
            }
        }
        
        function submitSearch() {
            var searchForm = byId("search_form");
            //.... other random checks...
            if (searchForm) {
            	searchForm.submit();
            }
            
            // stop form from submit twice
            return false;
        
        }
        }
        I didn't test this, but it should work.

        Cheers

        G

        Comment


        • #5
          I don't know how JavaScript is supported in latest browsers etc, but i thought you had to do:

          Code:
          if(window.event)
            key = window.event.keyCode;     //IE
          else
            key = e.which;     //firefox
          to achieve cross browser support

          G

          Comment


          • #6
            the only difference you made was adding
            Code:
            // stop form from submit twice
                return false;
            right? If so, that didn't help. I still get the duplicate onSubmit calls.

            I still haven't confirmed through thurough testing, but it seems that these duplicate calls tend to only happen when I submit the form via the button, get/display the results, then submit the form again via the Enter key in the textbox


            re: cross-browser - one of the good and bad things about this project is that I only have to support IE6 (which is probably why it took me so long to notice this duplicate submit issue because most of my development is done in Firefox...)

            Comment

            Working...
            X