Jump to content

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


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 post
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 post
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 post
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()

Edited by thescientist
Link to post
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()}

Edited by confused and dazed
Link to post
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.

Edited by ShadowMage
Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...