Results 1 to 5 of 5

Thread: how to have 1 form with 2 submit buttons

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Posts
    237

    how to have 1 form with 2 submit buttons

    hi all, how do you make 1 form with 2 submit buttons? I can show the buttons, of course, but how to handle the code?

    I have a form that contains a number of items with checkboxes, and when the user clicks Delete button, the checked items will be deleted. It's no problem because I can just use the code <form action=delete.php ....>.

    Now, the problem is, I want to make another button besides the Delete button. I want to have a Move button, so that when the user clicks the Move button, the checked items in the form will be moved to another category, not delete them.

    I'm confused how to do that. You can't have a form with two actions (delete.php and move.php), right?

    thanks for your help

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: how to have 1 form with 2 submit buttons

    The best way to do this is to submit to one script and create an action array form the submit buttons:
    Code:
    <input type="submit" name="action[delete]" vlaue="Delete" />
    <input type="submit" name="action[move]" value="Move" />
    You can then use the following code to check for what action is required:
    PHP Code:
    if (is_array($_POST['action'])) {
        
    $action array_keys($_POST['action']);
        
    $action $action[0];
    }

    swtich ($action) {
        case 
    'move':
            
    do_move();
            break;
        case 
    'delete':
            
    do_delete();
            break;

    Some would say, just check the value the action variable, however, if you ever wanted to rename your submit button you will have to rewrite your code.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: how to have 1 form with 2 submit buttons

    Actually, I tend to use <button> elements in these situations.
    Code:
    <button type="submit" name="action" value="move">Caption here</button>
    <button type="submit" name="action" value="copy">Other caption here</button>
    No ugly hacks, no rewriting.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: how to have 1 form with 2 submit buttons

    Woopz, didn't read the topic good enough. (Deleted my reply )
    Last edited by BramVandenbon; Dec 9th, 2005 at 06:25 PM.
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  5. #5
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802

    Re: how to have 1 form with 2 submit buttons

    Here's anoher way to do it: http://thomasdamgaard.dk/pub/html/multiple-submit/
    It uses a bit of java script to submit to different pages dependig on which button was pressed
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width