how to create multiple submit button in a php form and redirect its data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ChristinaTIT
    New Member
    • Sep 2008
    • 3

    #1

    how to create multiple submit button in a php form and redirect its data

    Hi,

    I have a form containing

    ID
    Type
    Quantity
    Rate

    values. it should have two submit buttons which, when posted, will do the two different things in two different scripts.

    how to create multiple submit button in a php form and redirect its data to a selected script ?
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Christina.

    Give each submit button a name:
    [code=html]
    <input type="submit" name="submitted " value="Send to Friend" />
    <input type="submit" name="submitted " value="Send to Random Hooligan" />
    [/code]

    Then use the value of the submitted input on the PHP side:
    [code=php]
    switch( $_POST['submitted'] )
    {
    case 'Send to Friend':
    .
    .
    .
    break;
    }
    [/code]

    Comment

    • phpNerd01
      New Member
      • Sep 2008
      • 8

      #3
      Onclick you change the action value of the form ;)

      [code=html¨]

      <html>

      <FORM action="mulsub1 .php" method="post">
      First name: <INPUT type="TEXT" name="FNAME"><B R>

      <INPUT type="submit" name="bsubmit" value="Submit 1" onclick="action ='mulsub1.php'; ">
      <INPUT type="submit" name="bsubmit" value="Submit 2" onclick="action ='mulsub2.php'; " >
      <INPUT type="submit" name="bsubmit" value="Submit 3" onclick="action ='mulsub3.php'; ">
      </FORM>

      </html>

      [/code]


      Hope it helps!
      phpNerd01

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        The main issue with phpNerd01's solution is that it requires Javascript. If a User has Javascript disabled, the form will not behave properly.

        Comment

        • coolsti
          Contributor
          • Mar 2008
          • 310

          #5
          What pbmods says concerning a user having Javascript disabled is correct, but .....

          a bit of javascript on the client side allows you to do so much more with your page, and makes a lot of actions easier to perform. One of which is the solution to your problem suggested by phpnerd01 above, which is the way I also do things.

          Therefore, it is probably better to create your website and pages in such a way that Javascript is essential for it to perform. That way the user needs to enable Javascript to get anywhere. In my opinion, if a user today wishes to disable Javascript or uses a browser that does not use Javascript, then that user has no business looking at my pages :)

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Originally posted by coolsti
            Therefore, it is probably better to create your website and pages in such a way that Javascript is essential for it to perform. That way the user needs to enable Javascript to get anywhere. In my opinion, if a user today wishes to disable Javascript or uses a browser that does not use Javascript, then that user has no business looking at my pages :)
            Because of XSS, it is not uncommon for Users to disable Javascript altogether or to use NoScript to selectively disable it.

            There are also still a significant number of PDA and cell phone browsers that do not support Javascript. And visually-disabled Users might be using a speech-based or otherwise-specialized browser that cannot execute Javascript.

            Saying that Users have to have Javascript turned on to use your site is all fine and good, but is it worth it to to have to maintain code that functions differently in each browser just so you can alienate 4% of your User base?

            Comment

            • coolsti
              Contributor
              • Mar 2008
              • 310

              #7
              Well, this is not the place for such a discussion. It all depends on the application and the audience. My audience has not been general public, but company wide users, and so I feel I can demand that Javascript be enabled and a browser that is compatible with what I develop my pages for should be used.

              Comment

              • Ivan Carrasco Q
                New Member
                • Apr 2011
                • 2

                #8
                I did it using the follow idea:

                <?php
                //file: page.php
                // you don't need to create other PHP files.

                echo "<form method='post' action='page.ph p'>";
                echo " <input type='submit' name='agree' value='agree'/>";
                echo "</form>";
                echo "<form method='post' action='page.ph p'>";
                echo " <input type='submit' name='disagree' value='disagree '/>";
                echo "</form>";

                if($_POST['agree'])
                {
                echo "You AGREE";
                }

                if($_POST['disagree'])
                {
                echo "You DISAGREE";
                }
                ?>


                I hope it helps!
                :-)
                Last edited by Ivan Carrasco Q; Apr 27 '11, 03:42 AM. Reason: sorry! mistake

                Comment

                Working...