Web Analytics Made Easy -
StatCounter PHP, more versitile everyday! - CodingForum

Announcement

Collapse
No announcement yet.

PHP, more versitile everyday!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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.
    Create accessible online surveys -::- Koobten.com - compare netbook prices and reviews -::- Affordable, reliable hosting for less than $20 per year
    Zend Certified Engineer


  • #2
    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
    CodingForum Supreme Overlord
    All Hail Spookster

    Comment


    • #3
      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

      Comment


      • #4
        ... 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)

        Comment


        • #5
          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...

          Comment


          • #6
            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
            }
            ?>

            Comment


            • #7
              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.

              Comment


              • #8
                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

                Comment


                • #9
                  Heh, this is an old thread, almost as old as le spook

                  Comment


                  • #10
                    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

                    Comment

                    Working...
                    X