Jump to content

[SOLVED] How to catch form-submission events?


kratsg

Recommended Posts

So, I've got this little project in which I'm making a script which performs almost similarly (in functionality) to PHP with form submissions, and stores information in cookies to pass over to new pages. (Reason being we can't get a server at school, they won't let us, so I'm making this so we can actually have the other classes learn how forms work).

 

My basic question is, let's say there are multiple forms, how can I catch which form was submitted so I can process the data only for that form?

 

So far, I've got a system set-up in which you give it the form that was submitted, it runs a function collecting everything in that form with a specific class, pairing up the names-to-values and saving them in cookies. This works well, if you tell it the form, but I want it to work without any help by the user (who would know no javascript). That is, have the javascript read the page, know how many forms there are, determine which form gets submitted, and process it.

Link to comment
Share on other sites

No, what can (and will) happen is that multiple forms will appear on the page. When the user clicks to submit any of the forms (one, not all); I want the javascript to be able to catch that event, and process it.

 

I just can't figure out how to catch that event.

Link to comment
Share on other sites

 

you want your php to know which form it is? is that what your trying to do?

 

well if that is what your trying to do; give each of your forms a hidden input field and name each field something like "formselector" and then give each of the different hidden input fields a value for whatever you want the form to be called; do this with each of your forms. then set up your php script to receive this "formselector" variable. this way you can distinguish between what form sent you the data to the php script.

Link to comment
Share on other sites

 

you want your php to know which form it is? is that what your trying to do?

 

well if that is what your trying to do; give each of your forms a hidden input field and name each field something like "formselector" and then give each of the different hidden input fields a value for whatever you want the form to be called; do this with each of your forms. then set up your php script to receive this "formselector" variable. this way you can distinguish between what form sent you the data to the php script.

 

This doesn't help. Where do you even get php script from? Read my post before answering... This isn't helping me at all.

Link to comment
Share on other sites

Well since I was edited; I will say it in a nicer way (I would hate to offend someone who is being rude to me ober - that is sarcasm by the way, in case you didn't know); you will not be getting any help from me on this; because you cannot talk to people in a nice way. We are not here to meet your demands; we here to assist your for free. We don't get paid to provide you a service. If you continue to talk to people in that fashion kratsg; no one is going to ever help you on here.

Link to comment
Share on other sites

Well since I was edited; I will say it in a nicer way (I would hate to offend someone who is being rude to me ober - that is sarcasm by the way, in case you didn't know); you will not be getting any help from me on this; because you cannot talk to people in a nice way. We are not here to meet your demands; we here to assist your for free. We don't get paid to provide you a service. If you continue to talk to people in that fashion kratsg; no one is going to ever help you on here.

 

I wasn't even being rude. I was pointing out that we're in the JavaScript forum and that (if I could) I would use PHP, but I said in the first post I couldn't because the school will not let us have a server for testing purposes... So I'm creating a javascript that has the functionality of php... without the php.

Link to comment
Share on other sites

This doesn't help. Where do you even get php script from? Read my post before answering... This isn't helping me at all.

 

uh - yeah you were being rude; let me point out you little rude and obnoxious statement above.

 

So I'm creating a javascript that has the functionality of php... without the php.

 

good luck with that pal; cause I will not be helping you with it.

Link to comment
Share on other sites

So you are submitting a form and processing its data to a cookie and you want to be able to do this for multiple forms on a page without the page refreshing or going to the page/script that you have defined in the action attribute?

 

Well, in a sense. The form will be set up the same way as you would if you were submitting php (action and method attributes and all). I want the javascript to be able to "catch" the form submission event (if that even exists?), stop it in its tracks, and take over the processing. The problem I have is that I can't control what forms will be on the page, or how many there are, so in a sense, the javascript should be able to do the above, but for the form that's submitted.

Link to comment
Share on other sites

function formListener(){    
    for(var x = 0, len = document.forms.length; x < len; x++){
        document.forms[x].onsubmit = function(){
            //put your cookie code here
            return false; //stop the form from submitting;
        };
    }
}

onload = formListener;   

Link to comment
Share on other sites

function formListener(){    
    for(var x = 0, len = document.forms.length; x < len; x++){
        document.forms[x].onsubmit = function(){
            //put your cookie code here
            return false; //stop the form from submitting;
        };
    }
}

onload = formListener;   

 

Event listeners, the one thing in JavaScript I've never really learned yet (I'm a self-learner all the way :-P). Hmm, but how does it actually catch the event? The page loads, but that doesn't mean a form is submitted, or am I thinking about this backwards?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.