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 10 of 10
  1. #1
    New Coder
    Join Date
    Jul 2002
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb detecting form submissions

    I need to execute a javascript function every time a form is submitted. If the form is submitted using a submit button, then an onsubmit event occurs. However, i need to be able to execute some javascript even when the form is submitted with the form.submit() function. Is there any way for me to do this (without altering the code that calls form.submit())? Thanks.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    New York, USA
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <form ..... onSubmit="return myFunc();">

    <script>
    function myFunc(){
    //code here to execute before submission
    return true;
    }
    </script>

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    boywonder, onsubmit is only triggered when the submit button (input type=submit not input type=image) is clicked but not programmatically when submit() method is called as experienced by virtualshock.

    virtualshock, the solution is to call the function you want before executing form.submit()

    Originally posted by boywonder
    <form ..... onSubmit="return myFunc();">

    <script>
    function myFunc(){
    //code here to execute before submission
    return true;
    }
    </script>

  • #4
    New Coder
    Join Date
    Jul 2002
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Thanks

    If only it were that simple . Fortunately, I managed to figure out a solution that's a bit similar to yours.

    In case you're curious, I couldn't use your solution directly, because I couldn't change the functions. The page on which the script would run is globally included in all other pages, so it has to dynamically check for any submissions, be they from a submit button, an image, or a link, etc... To make a long story short, it took about 49 lines of code, but it works. Thanks for the help tho

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    Think this works...

    <html>
    <head>
    <title>untitled</title>
    <script type="text/javascript" language="javascript">

    function check_valid(form) {
    if ((!form.validated && form.onsubmit()) || form.validated) {
    form.action = 'javascript:alert("Submitted !")';//stick a real url here!
    form.submit();
    }}

    function validate(form) {
    alert('Validator called...'); //lose this
    form.validated = false; //flag
    if (!form.t.value) {
    alert('No text.');
    form.t.focus();
    return false;
    }
    return (form.validated = true);
    }

    </script>
    </head>
    <body>
    <form name="f"
    action="javascript:check_valid(document.f)"
    onsubmit="return validate(this)">
    <input name="t"><br><br>
    <input type="submit" value="submit from submit button"><br>
    <a href="javascript&#58;void document.f.submit()">submit from link</a>
    </form>
    </body>
    </html>

  • #6
    New Coder
    Join Date
    Jul 2002
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up You're a genious

    Thanks, adios. You just saved me 30+ lines of code. It never occurred to me to change the action of the form.

  • #7
    New Coder
    Join Date
    Jul 2002
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question One more thing

    In netscape 4, if I set the action to "javascript:myfunction(param,param)", the function doesn't get called. Instead, the url changes to javascript:myfunction(param,param). It works on netscape 6, IE 4, 4.5, 5, and 6. How do i get around this? Thanks.

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    One of the stupidest of all Navigator bugs - brought to you by the people who invented JavaScript....when a form's method is "get" (the default) and a javascript&#58; url is the action, the browser appends the query string - to the JS! Idiotic...change the method to "post", and re-set it in the function:

    function check_valid(form) {
    if ((!form.validated && form.onsubmit()) || form.validated) {
    form.action = 'submission_URL';
    form.method = 'get';
    form.submit();
    }
    }
    Last edited by adios; 07-27-2002 at 10:47 PM.

  • #9
    New Coder
    Join Date
    Jul 2002
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy more questions

    does the bug get fixed when you change the method using form.method="post", or does it have to be done through the html tag? I'm trying to view the page on NS 4.72 on a Mac and the bug fix isn't working. Thanks Again.

  • #10
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    Has to be done via HTML:

    <form name="f" method="post"
    action="javascript:check_valid(document.f)"
    onsubmit="return validate(this)">

    You're changing the initial submission method, in order to keep the querystring from being appended (to the submission url, which, in this case, is JS code); after validation, you reset it. All this assumes you're submitting method="get"; if you're not, shouldn't be a problem.

    cheers, adios


  •  

    Posting Permissions

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