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.

7 Responses to “Javascript - two submit buttons - two seperate purposes”

  1. Happy Says:

    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?’)” >

  2. Jennifer Says:

    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. :)

  3. insin Says:

    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();
    }
    }

  4. JUan Says:

    Your tutorial are good, easy to understand!
    Thank You, very much.

  5. Anonymous Says:

    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 “

  6. Jennifer Says:

    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.

  7. Levent Demir Says:

    that is what i want

Leave a Reply