Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2017
    Posts
    197
    Thanks
    2
    Thanked 0 Times in 0 Posts

    js code not executing on form submission

    I have the following form tag
    Code:
    <form method="post" enctype="multipart/form-data" name="Register" id="Register" onsubmit="CheckData();" action="Register.php">
    submit
    Code:
    <input type="submit" name="Submit"  >
    I have both js and php error checking in the same file, Register.php. CheckData() is not being executed on form submission, just php. I have an alert at the top of the js code to show its being executed, its not firing. Is it clashing with the submit button?

  2. #2
    Regular Coder
    Join Date
    Aug 2017
    Posts
    197
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Got the alert firing, so onsubmit() is working.

  3. #3
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,513
    Thanks
    4
    Thanked 506 Times in 494 Posts
    When you use the onsubmit attribute (which you shouldn't, has no business in the HTML) your code inside the attribute must return true for the submit to happen, meaning you either make the function return true/false and return the function, or you do the return in the attribute.

    onsubmit="CheckData(); return true;"

    Again though, NOT that you should be putting JavaScript in to the markup like that -- what with the onevent attributes being a XSS risk, pointless code bloat for users who have scripting blocked, and don't even EXIST and are outright blocked if you deploy under the CSP.

    Which is why your scripting should be hooking the form with addEventListener. (and an attachEvent polyfill for legacy IE if needed.)
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •