Jump to content

How Do I Get Two Different Functions To Run When Submitting A Form?


confused and dazed

Recommended Posts

Hello internet. I would like to start off by saying that W3Schools forum and all you great programmers that respond to these topics is INVALUABLE!!! Thank you so much. Here is my problem... I currently have only one form for my site<form name=tourney action="m m_post_process_4.php" method="post" onSubmit="return checkit()" target="_blank"> When all the requirements have been met (through the checkit function) the form is sent to the PHP file for processing. However, I have two submit buttons and would like each button to process a different function - one for checkit and one for checkit2. This may seem odd because you may say combine the two functions into one function but the problem I am running into is that I cannot seem to get the element ID during a submit action. If I could I would combine the two functions and run comparisons to do all the work. However, the PHP output keeps telling me "Undefined1" for the ID value I am trying to compare. Any thoughts?

Link to comment
Share on other sites

I think I understand - do you mean do the following to the form text <form name=tourney action="m m_post_process_4.php" method="post" target="_blank"> and then add unique onclick functions to the different submit buttons?
Yes. I think if you return false in the onclick functions, it will prevent a form submission as well, not sure though. You'll have to test it. If that doesn't work, you could use regular buttons instead of submit buttons (ie, use the button type instead of the submit type). Then, when you want to submit the form, you'll just put form.submit() where form is a reference to the form (give the form an id to make getting the reference easy).
Link to comment
Share on other sites

I used regular buttons but the form did not submit. Everything else worked perfectly (I mean all the checks I have). Where do I add the tourney.submit() to my function checkit and checkit2? My form text is:<form name=tourney action="m m_post_process_4.php" method="post" target="_blank"> My button text is:<input type="button" name="Preview" value="Preview"; onclick="checkit();" /><input type="button" name="Submit" value="Submit"; onclick="checkit2();" /> Where do I add the tourney.submit() to get the forms to submit to the PHP file?

Link to comment
Share on other sites

you would know better, since you haven't shown us the checkit functions. basically you should track the code and at the end of the function(s), if everything passes, then you can submit the form.

document.getElementById('tourney').submit()

Link to comment
Share on other sites

Well I said it too quick...It does not like my Checkit2 function where I am trying to have a pop up box ask the user if they want to submit the form. It does not submit the form - the popup box comes but the form does not get submitted? Why? function checkit2(){var finalconfirmation = true;if (finalconfirmation){ var result = confirm("Form Complete. Do you want to submit your Form?"); if (!result) { finalconfirmation = false; }return (finalconfirmation)}document.getElementById('tourney').submit()}

Link to comment
Share on other sites

What is happening? Do you get any errors? EDIT:Wait, I'll bet the issue is that it submits the form regardless of the prompt. The line that submits the form should be in an else clause for the if statement that checks the result from the prompt. BTW, you really don't need to return anything from these functions unless you're using it for something other than trying to prevent submission. A regular button will never submit the form unless you explicitly tell it to with the submit() function.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...