homepage Welcome to WebmasterWorld Guest from 54.163.139.36
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Auto-detecting Form Submission
ryan_b83

5+ Year Member



 
Msg#: 3581579 posted 9:23 pm on Feb 21, 2008 (gmt 0)

Hello, I was wondering if there is an easy way to automatically detect if a form has been submitted on a page. Basically I need to run a JS function on every form submission on many pages. Now i know i can add in the <form> tag the following attribute:

onclick="runFunction();"

However I am trying to make this happen throughout my whole website consisting of many forms in many places including a custom made CMS. So with that said I would like to try to add some JS to the head of the page which will detect a form submission. I was thinking maybe setting an event handler to run ever second to see if a form was submitted, but I wasn't sure if that was possible or even the best way to do it?

Thanks,
Ryan

 

Otaku

5+ Year Member



 
Msg#: 3581579 posted 9:38 pm on Feb 21, 2008 (gmt 0)

Instead of onclick, you better add the onSubmit event. The function that you call there must return true for the form to submit, otherwise the browser will do nothing.

Personally I don't know any other events that can arise when a form is being submitted. So I think you should add the attribute to every form anyway.

ryan_b83

5+ Year Member



 
Msg#: 3581579 posted 3:03 pm on Feb 22, 2008 (gmt 0)

Yes my mistake, i ment "onSubmit"... but there is no way in Javascript to automatically detect the submission of a form?

Achernar

5+ Year Member



 
Msg#: 3581579 posted 4:59 pm on Feb 22, 2008 (gmt 0)

<script>
function fSub(f) {

}
</script>

<form onsubmit="return fSub(this)">

fSub() knows automatically when the form is submitted. And it can prevent submission or modify the form values before submitting.

mehh

5+ Year Member



 
Msg#: 3581579 posted 6:24 pm on Feb 22, 2008 (gmt 0)

Something like this?
addEvent=(addEvent)?addEvent:function(el,ev,fn){ 
if(el.addEventListener){
el.addEventListener(ev,fn,false);
}
else if(el.attachEvent){
el.attachEvent("on"+ev,fn);
}
else{
el["on"+ev]=fn;
}
}
addEvent(window,"load",function (){
var fs=document.forms;
for(var i=0;i<fs.length;i++){
addEvent(fs[i],"submit",[b]yourFunction[/b])
}
});

ryan_b83

5+ Year Member



 
Msg#: 3581579 posted 9:29 pm on Feb 22, 2008 (gmt 0)

PERFECT THANKS!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved