1

I am new to web dev and for a school exercise, I'm working on something where I want to execute javascript of form submit event.

<head>
  <script>
    function foobar() {
      alert('Form submit event hit!');
    }
  </script>
</head>

<body onload="document.myForm.submit()">
  <form name="login" id="login" action="login.php" method="POST" onsubmit="foobar();">
    <input type="hidden" name="username" .../>
    <input type="hidden" name="password" .../>
    <input type="submit" name="submit" value="submit" />
  </form>
</body>

When i execute this, I see that form gets submitted successfully that I see the output after successful login. But, I don't see the submit event's handler Java script getting called or hit. I'm sure I'm missing something. Any help is appreciated.

Quentin
  • 857,932
  • 118
  • 1,152
  • 1,264
smaven
  • 21
  • 3
  • I start with x.html that redirects to login.php/login form. But when login form "Login" button is clicked, which is 'submit' type I cannot call the handler function, even if I specify via onsubmit='' option of form. – smaven Dec 03 '19 at 17:40
  • Add `event` argument for `onsubmit="foobar(e);"` and `function foobar(e) {...}`. Then use it in your function. – Serhii Zharkov Dec 03 '19 at 17:43
  • thanks. But didn't help. – smaven Dec 03 '19 at 17:53

1 Answers1

-1

The onsubmit function IS getting called after clicking submit button. Check my jsfiddle out.

https://jsfiddle.net/dthsLey7/11/

<!DOCTYPE html>
<html>

<body onload="document.myForm.submit()">
    <form name="login" id="login" action="login.php" method="POST" onsubmit="foobar();">
        <input type="hidden" name="username" .../>
        <input type="hidden" name="password" .../>
        <input type="submit" name="submit" value="submit"/>
    </form>
</body>

<script>
function foobar()
{
    alert('Form submit event hit!');
}
</script>
</html>
Zoran777
  • 552
  • 1
  • 7
  • 25
  • Yep. The code is [working just fine for me](https://www.dropbox.com/s/m00r531mwn94kka/Screenshot%202019-12-03%2012.52.30.png?dl=0) as is ‍♂️. Probably a browser related issue. Have you tried incognito mode? – selfagency Dec 03 '19 at 17:55
  • If you can't reproduce a problem, you should call it out in a comment. This isn't an answer. – Quentin Dec 03 '19 at 18:03
  • strange. not working for me. I tried private window on Firefox. – smaven Dec 03 '19 at 18:05
  • @Quentin In this scenario, it was necessary. Since the OP is new to web development and I think it's better they can see it for themselves. I don't think it deserves a downvote. This is what makes stackoverflow unwelcoming towards devs. Both newbies and those who are experienced. – Zoran777 Dec 12 '19 at 11:40