...

View Full Version : detecting form submissions



virtualshock
07-25-2002, 09:40 PM
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.

boywonder
07-25-2002, 10:56 PM
<form ..... onSubmit="return myFunc();">

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

glenngv
07-26-2002, 03:02 AM
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>

virtualshock
07-26-2002, 03:52 AM
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 :thumbsup:

adios
07-26-2002, 04:01 AM
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>

virtualshock
07-26-2002, 08:20 PM
Thanks, adios. You just saved me 30+ lines of code. It never occurred to me to change the action of the form.

virtualshock
07-27-2002, 12:47 AM
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.

adios
07-27-2002, 12:55 AM
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();
}
}

virtualshock
07-27-2002, 01:52 AM
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.

adios
07-27-2002, 10:53 PM
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 :cool:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum