Javascript - two submit buttons - two seperate purposes
Needed this for a script I’m working on where one button will be doing one thing, another will be deleting stuff. So I needed a button that had a “confirm” message before going ahead and deleting stuff - but both buttons needed to submit the form (I would check the VALUE of the submit button via php). This script shows how to tie a confirm message to one button.
June 6th, 2003 at 12:58 pm
It probably would’ve been cleaner to just put the confirm() function in the onClick attribute instead of calling a user-defined function, saving the response to a variable, testing the variable, and then returning true or false.
confirm() returns a true or false anyway, so the whole thing could be reduced to:
<input type=”submit” value=”Delete” onClick=”return confirm(’Are you sure?’)” >
June 9th, 2003 at 8:23 am
I guess the only benefit to it being OUTSIDE of the tag itself, is in the case you have a really long confirm message. “You are about to destroy your entire blog. There is no turning back. Are you really absolutely positvely sure you want to delete your entire blog, all your hard work over so many years?”
Or maybe if there’s more than one form that has the same confirm message? or if you want to include other validation stuff…?
There’s probably other scenarios I can think of where this would be usefull… but it’s good to know if it only needs a simple solution - one is available.
June 10th, 2003 at 6:08 am
Another way to have more than one submit button - use a button’s onclick to call a JavaScript function to change the form’s action then submit it.
eg. I have a form which can be used to update or delete an item. The hard-coded form action will update the item when you click on the submit button (labeled “Update”), while I have the “Delete” button call this function:
function doDelete()
{
if(confirm(”Do you want to delete this Pre-Requisite Software Item?”))
{
document.preReqForm.action = “DeletePreReqItem.do?section=PreReqSoft&tab=tab3″;
document.preReqForm.submit();
}
}
July 25th, 2003 at 4:55 pm
Your tutorial are good, easy to understand!
Thank You, very much.
September 13th, 2003 at 9:03 pm
Huh, I don’t see the correlation?
Re:
“Google Reference
I noticed you arrived here via Google looking for seperate.
To aid you on your search, here are the top results for your keywords:
August 12, 2003 - “Hi” Script
June 26, 2003 - Javascript: Exploding a string into an array
June 20, 2003 - Comment Queue Script/MT hack UPDATED
June 10, 2003 - Coment Queue Script/MT Hack “
September 13th, 2003 at 9:09 pm
That is a script completely unrelated to this post that tries to help people coming from Google by searching the rest of the site for similar keywords. Your keyword was “Seperate” - seperate is in the title of this post - as it is probably also in those other posts. It’s more for when people are searching for specfic scripts, and they happen to come to a page that may not be the best fit.
April 8th, 2004 at 6:17 am
that is what i want