kratsg Posted January 28, 2008 Share Posted January 28, 2008 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. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 28, 2008 Share Posted January 28, 2008 are you submitting all of your multiplet forms at once; like with one form action or javascript function? Quote Link to comment Share on other sites More sharing options...
kratsg Posted January 28, 2008 Author Share Posted January 28, 2008 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. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 28, 2008 Share Posted January 28, 2008 you want your php to know which form it is? is that what your trying to do? Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 28, 2008 Share Posted January 28, 2008 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. Quote Link to comment Share on other sites More sharing options...
kratsg Posted January 28, 2008 Author Share Posted January 28, 2008 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. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 28, 2008 Share Posted January 28, 2008 This doesn't help. Where do you even get php script from? Read my post before answering... This isn't helping me at all. Well I guess I didn't read your post exactly right, *snip* edit by ober: there seems to be confusion on both sides. Please keep it civil and avoid name calling. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 28, 2008 Share Posted January 28, 2008 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. Quote Link to comment Share on other sites More sharing options...
kratsg Posted January 28, 2008 Author Share Posted January 28, 2008 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. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 29, 2008 Share Posted January 29, 2008 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. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted January 29, 2008 Share Posted January 29, 2008 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? Quote Link to comment Share on other sites More sharing options...
kratsg Posted January 29, 2008 Author Share Posted January 29, 2008 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. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted January 29, 2008 Share Posted January 29, 2008 Alright well that is pretty simple. You can collect all of the forms on the page by document.forms. Loop though that array and add an event listener for the submit action. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted January 29, 2008 Share Posted January 29, 2008 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; Quote Link to comment Share on other sites More sharing options...
kratsg Posted January 29, 2008 Author Share Posted January 29, 2008 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? Quote Link to comment Share on other sites More sharing options...
emehrkay Posted January 30, 2008 Share Posted January 30, 2008 I dont think I understand your question. Quirksmode has a great write up on how events work in the browser and how you should assign listeners or functions http://www.quirksmode.org/js/introevents.html Quote Link to comment Share on other sites More sharing options...
kratsg Posted January 30, 2008 Author Share Posted January 30, 2008 Perfect, just what I needed. I was thinking about it backwards. No wonder I got confused xD Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.