Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts

    PHP, more versitile everyday!

    I just discovered this little how-to-do in php that I though I would share.

    I didn't know that you could have multiple submit buttons in a form and determine which one was clicked on in php. Example:
    Code:
    <html>
    <body>
       <form action="myPhp.php" method="post">
          <input type="text" name="fName">
          <input type="submit" name="update" value="Update Name">
          <input type="submit" name="delete" value="Remove My Name">
       </form>
    </body>
    </html>
    and the php file goes like this...

    Code:
    <?php
    if(isset($update)) {
      print "You're name has been updated";
      //do other stuff
    }
    else if(isset($delete)) {
      print "You have been removed";
      //do other something
    }
    else {
      print "Huh, I didn't get that? What do you want to do???";
      //default something
    }
    ?>
    I'm sure you php gurus knew this, but I just discovered it last night in a trial and error frenzy (ie debugging) session.

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    I knew you can detect the name of the button pushed but I hadn't thought of using it in that way. Nice idea. Thanks for sharing.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Regular Coder
    Join Date
    May 2002
    Location
    London, England
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can indeed. Just another little thing yo might find useful...

    if($delete) is the same as if(isset($delete))

    Jee
    Jeewhizz - MySQL Moderator
    http://www.sitehq.co.uk
    PHP and MySQL Hosting

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,106
    Thanks
    11
    Thanked 101 Times in 99 Posts
    ... but sadly (IMO) you need to get used to ..

    $_POST[delete];
    or
    $_HTTP_POST_VARS[delete];

    best start now as soon enough you will have no choice ;(
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    183
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is that what they have in store for the next PHP?! Geez, that'll be a huge pain for a whole lot of people. Or no one will update.
    Offtone.com - In the works...

  • #6
    New Coder
    Join Date
    Jun 2002
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I hear ya - that's silly in my opinion.

    You can also have multiple buttons with the same name, and determine the action by the value.. like..

    <html>
    <body>
    <form action="myPhp.php" method="post">
    <input type="text" name="fName">
    <input type="submit" name="action" value="Update">
    <input type="submit" name="action" value="Cancel">
    <input type="submit" name="action" value="Remove">
    </form>
    </body>
    </html>

    <?
    if($action == 'Update') {
    print "You're name has been updated";
    //do other stuff
    }
    else if($action == 'Delete') {
    print "You have been removed";
    //do other something
    }
    else {
    print "Canceling....";
    //default something
    }
    ?>

  • #7
    Regular Coder
    Join Date
    May 2004
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts

    good use of buttons

    this is a good post!

    i also have used that method but with checkboxes instead. i think i'll use buttons instead because it looks nicer on a form. its a good idea to always include an else statement at the end in case neither button is selected.

  • #8
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fitzbean
    IYou can also have multiple buttons with the same name, and determine the action by the value
    This is what i always do, because in a multi-purpose page, you then only need to check if for instance $_POST['action'] is set and if not, go directly to your formbuilding. If set, then i use a switch like

    switch ($_POST['action']){
    case 'delete':
    ...
    break;
    case 'update':
    ...
    break;
    default:
    $info = 'No action selected. Form is reloaded.';
    }

    to determine which part of the formprocessing needs to be exectued.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #9
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Heh, this is an old thread, almost as old as le spook

  • #10
    Regular Coder
    Join Date
    May 2004
    Location
    sweden
    Posts
    236
    Thanks
    0
    Thanked 0 Times in 0 Posts

    The world has gone mad

    Because PHP programmers can't get it through threir heads to use $_Request a lot of ISPs are going with the default of register_globals on again.

    It is really stupid and annoying to see that they have upgraded PHP consistently and then suddenly register_globals is on again. But this guy has the right solution to getting it straight before you roll out your code to multiple sites like I did.

    http://martin.f2o.org/php/portable
    Carl McDade
    _____________
    Hiveminds Magazine
    for web publisher and community builders
    eRuby Tutorials


  •  

    Posting Permissions

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