-1

I'm using javascript and html, here is my code, not exact code, the form body is pretty big so I am not sharing it:

Problems I'm facing:

  1. Page refreshes when I submit the form.
  2. I have 2 forms in my page and "return false" works on the 1st from but not on the 2nd.

HTML

        <div class="col_8 signin" id="s1">
            <form name="sigin" onsubmit="login(); return false" method="post">
            //FORM CONTENT//    
            </form>
       </div>

        <div class="col_8 signup" id="s2">
            <h1>SIGN UP</h1>
            <form name="sigup" onsubmit="submit(); return false" method="post">
            //FORM CONTENT//  
            </form>
       </div>
  • 1
    Does this answer your question? [How to prevent form from being submitted?](https://stackoverflow.com/questions/3350247/how-to-prevent-form-from-being-submitted) – Sfili_81 Apr 12 '22 at 08:11
  • Bottom line: don't use `onxxxxx` attributes with all its pitfalls, use event listeners instead. – CherryDT Apr 12 '22 at 08:15

1 Answers1

2

Calling a form's submit method will submit the form while bypassing event listeners.

So with this code:

onsubmit="submit(); return false"
  1. Something triggers a normal form submission
  2. The event handler triggers an unblockable form submission
  3. return false cancels the submission at 1
  4. The submission from 2 goes ahead

It is possible you are trying to call a different function called submit and are hitting the form's submit method by accident because intrinsic event attributes are awful and screw up scope.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335