SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot maxdream01's Avatar
    Join Date
    Feb 2011
    Location
    USA
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Multiple submit buttons for different things

    Hey everyone, I'm working on an editor for a blog and part of the functionality is to be able to, in the same form, either save changes, delete the entry or cancel. Now, i can think of a dozen ways to do this in javascript , but i was wondering if there was any easy, non-complicated way to do this purely in HTML, with some attribute or something like that.

    so in other words i'd like to have multiple submit buttons in the same form point to different actions. If its not possible without Javascript, i don't have a problem with doing that, if thats the case just say so.

    Here is the form code if it helps in anyway:
    HTML Code:
    <form action="updateBlogPostHandler.php" method="post">
      <p>
      <label>Post Title</label>
        <input name="blogtitle" value="<?php print $title; ?>" alt="Title" type="text" id="blogtitle" size="100" maxlength="150"  />
        </p>
      <p><label>Blog Post</label></p>
      <p>
        <textarea name="blogpost" value="<?php print $post; ?>" cols="100" rows="5" id="blogpost"></textarea>
      </p>
      <input type="hidden" disabled="disabled" value="<?php $entry ?>" />
      <button type="submit">Save</button>
      <button type="submit">Delete</button>
      <button type="submit">Cancel</button>
    </form>
    Thanks
    PHP is FUN

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Give the buttons a name and a value, and check which one has been sent in your php code.

  3. #3
    SitePoint Zealot maxdream01's Avatar
    Join Date
    Feb 2011
    Location
    USA
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you
    PHP is FUN

  4. #4
    SitePoint Member dennis.g's Avatar
    Join Date
    Jun 2012
    Location
    Athens, Greece
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Give some extra thought on which is the first one. If the user is in the text field and hits enter, the system will assume the first submit button was hit.

  5. #5
    SitePoint Member
    Join Date
    Jul 2012
    Location
    Leicester
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Give them all different names and a value. They are only posted if they are clicked so you can do

    if($_POST['delete']){

    }

    if($_POST['save']){

    }

    etc

  6. #6
    Non-Member
    Join Date
    Jun 2012
    Posts
    88
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You might find it easier to give all the submit buttons the same name but different values. Then check in your php code which value was sent which will tell you which submit button was clicked.

    PHP Code:
    <?php
    if (isset($_POST['btnSubmit'][0])) {
        switch (
    strtolower($_POST['btnSubmit'][0])) {
            case 
    'save':
                echo 
    '<h1>Save button was clicked</h1>';
                break;
            case 
    'delete':
                echo 
    '<h1>Delete button was clicked</h1>';
        }
        die();
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
            <title></title>
        </head>
        <body>
            <form action="#" method="post">
                <input type="submit" name="btnSubmit[]" value="Save" />
                <input type="submit" name="btnSubmit[]" value="Delete" />
            </form>
        </body>
    </html>


Tags for this Thread

Bookmarks

Posting Permissions

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