2 submits buttons for a form.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    2 submits buttons for a form.

    Hey guys,
    Well what I want is to have one form, but two submit buttons. Depending on which submit button is pressed, different code will run.

    Here is a short version of my form:
    [HTML]<form action="myform. php" method="POST">
    <input name="var1" type="text" value="0" size="7" maxlength="6" />
    <input name="var2" type="text" value="0" size="7" maxlength="6" />
    <input name="var3" type="text" value="0" size="7" maxlength="6" />
    <input name="var4" type="text" value="0" size="7" maxlength="6" />
    <input name="recruit" type="submit" value="Recruit" />
    <input name="disband" type="submit" value="Disband" />
    </form>
    [/HTML]

    The php check:
    [PHP]if ( $_SERVER['REQUEST_METHOD '] == 'POST' && isset($_POST['var1']) && isset($_POST['var2']) && isset($_POST['var3']) && isset($_POST['var4']) )
    { //do stuff
    }[/PHP]

    So can I add something like && $_submit_name == 'recruit' ???

    Also, I know this is a php forum, but do you also know the javascript offhand:
    [HTML]if ( document.myform .var1.value == "" && document.myform .var2.value == "" && document.myform .var3.value == "" && document.myform .var4.value == "" )[/HTML]
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Originally posted by TheServant
    Hey guys,
    Well what I want is to have one form, but two submit buttons. Depending on which submit button is pressed, different code will run.

    Here is a short version of my form:
    [HTML]<form action="myform. php" method="POST">
    <input name="var1" type="text" value="0" size="7" maxlength="6" />
    <input name="var2" type="text" value="0" size="7" maxlength="6" />
    <input name="var3" type="text" value="0" size="7" maxlength="6" />
    <input name="var4" type="text" value="0" size="7" maxlength="6" />
    <input name="recruit" type="submit" value="Recruit" />
    <input name="disband" type="submit" value="Disband" />
    </form>
    [/HTML]

    The php check:
    [PHP]if ( $_SERVER['REQUEST_METHOD '] == 'POST' && isset($_POST['var1']) && isset($_POST['var2']) && isset($_POST['var3']) && isset($_POST['var4']) )
    { //do stuff
    }[/PHP]

    So can I add something like && $_submit_name == 'recruit' ???

    Also, I know this is a php forum, but do you also know the javascript offhand:
    [HTML]if ( document.myform .var1.value == "" && document.myform .var2.value == "" && document.myform .var3.value == "" && document.myform .var4.value == "" )[/HTML]
    I don't quite understand your coding.
    You check if your text inputs are set, but theyre always set.
    Also, i dont understand the usefullness of 'request_method '?

    Anyway, how i would do this is:
    [php]
    if(isset($_POST['recruit']))
    {
    # do recruit stuff
    }
    elseif(isset($_ POST['disband']))
    {
    # do disband stuff
    }
    else
    {
    # nothing submitted
    }
    [/php]

    Comment

    • Amzul
      New Member
      • Oct 2007
      • 130

      #3
      use javascript submit()
      each button onclick to send different data to the server

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Originally posted by Amzul
        use javascript submit()
        each button onclick to send different data to the server
        I cannot see how javascript could help here and work better than php.

        Just take the example markusn00b showed and you'll be fine. JS just clouds this dicusssion.

        Ronald

        Comment

        • TheServant
          Recognized Expert Top Contributor
          • Feb 2008
          • 1168

          #5
          Originally posted by markusn00b
          I don't quite understand your coding.
          You check if your text inputs are set, but theyre always set.
          Also, i dont understand the usefullness of 'request_method '?
          Cheers markus, yeah you're right about the request_method, it was an old form I was reformatting, forgot to take it out. I will try your code when I get home.

          I am concerned about your question about my text inputs...
          I have a few chunks of code to { do stuff } so when the form submits information to that file, it will send only the variables contained in that form. So by checking isset() I thought what I was doing was making sure it was the correct information for that function.

          ie.
          <body>
          form1 {var1 var2 var3}
          form2 {var4 var5 var6}
          </body>

          do_stuff.php {form1_function form2_function}


          If I submit form1, form1_function checks if var1, 2 and 3 are set (which they are) before it runs, but when the reader gets down to form2_function, var4, 5 and 6 are not set (because I only submitted form1) so it will not run.
          Is this not how it works?

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            See the below post

            Regards.
            Last edited by Markus; Apr 14 '08, 06:27 AM. Reason: I'm a fool!

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              Oh, i see.

              Well, you can bypass all that with using the way i suggested in my prev. post.

              Regards :)

              ps: i thought you were checking that the inputs had a value and all of them had to have a value before the IF statement would run - this is flawed because text inputs' are always passed in a form, empty or not.

              Comment

              • dlite922
                Recognized Expert Top Contributor
                • Dec 2007
                • 1586

                #8
                Originally posted by TheServant
                Hey guys,
                Well what I want is to have one form, but two submit buttons. Depending on which submit button is pressed, different code will run.

                Here is a short version of my form:
                [HTML]<form action="myform. php" method="POST">
                <input name="var1" type="text" value="0" size="7" maxlength="6" />
                <input name="var2" type="text" value="0" size="7" maxlength="6" />
                <input name="var3" type="text" value="0" size="7" maxlength="6" />
                <input name="var4" type="text" value="0" size="7" maxlength="6" />
                <input name="recruit" type="submit" value="Recruit" />
                <input name="disband" type="submit" value="Disband" />
                </form>
                [/HTML]

                The php check:
                [PHP]if ( $_SERVER['REQUEST_METHOD '] == 'POST' && isset($_POST['var1']) && isset($_POST['var2']) && isset($_POST['var3']) && isset($_POST['var4']) )
                { //do stuff
                }[/PHP]

                So can I add something like && $_submit_name == 'recruit' ???

                Also, I know this is a php forum, but do you also know the javascript offhand:
                [HTML]if ( document.myform .var1.value == "" && document.myform .var2.value == "" && document.myform .var3.value == "" && document.myform .var4.value == "" )[/HTML]
                Another way I do it, is the NAME of the buttons are the same but the values are not, so i just get the value of the button and run appropriate action.

                [PHP]

                <input type="submit" name="pageActio n" value="Action One" />
                <input type="submit" name="pageActio n" value="Action Two" />

                [/PHP]

                then on php side

                [PHP]

                if($_POST['pageAction'] == "Action One")
                {
                // do something
                }
                else if ($_POST['pageAction'] == "Action Two")
                {
                // do something else
                }

                [/PHP]

                Anybody know which way is better? I'll change the way markus is doin it if that's better.

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  Originally posted by dlite922
                  Another way I do it, is the NAME of the buttons are the same but the values are not, so i just get the value of the button and run appropriate action.

                  [PHP]

                  <input type="submit" name="pageActio n" value="Action One" />
                  <input type="submit" name="pageActio n" value="Action Two" />

                  [/PHP]

                  then on php side

                  [PHP]

                  if($_POST['pageAction'] == "Action One")
                  {
                  // do something
                  }
                  else if ($_POST['pageAction'] == "Action Two")
                  {
                  // do something else
                  }

                  [/PHP]

                  Anybody know which way is better? I'll change the way markus is doin it if that's better.
                  There won't be any significant difference; there's just more writing in your way :P

                  Comment

                  • TheServant
                    Recognized Expert Top Contributor
                    • Feb 2008
                    • 1168

                    #10
                    Didn't get a chance to try it, but both are logical. I will try both, but thanks for your help guys. Also interesting comment about text inputs markus, I didn't know that.

                    Comment

                    • Markus
                      Recognized Expert Expert
                      • Jun 2007
                      • 6092

                      #11
                      Originally posted by TheServant
                      Didn't get a chance to try it, but both are logical. I will try both, but thanks for your help guys. Also interesting comment about text inputs markus, I didn't know that.
                      Ah, no problem ma' man!

                      Comment

                      • TheServant
                        Recognized Expert Top Contributor
                        • Feb 2008
                        • 1168

                        #12
                        Originally posted by markusn00b
                        I don't quite understand your coding.
                        You check if your text inputs are set, but theyre always set.
                        Also, i dont understand the usefullness of 'request_method '?

                        Anyway, how i would do this is:
                        [php]
                        if(isset($_POST['recruit']))
                        {
                        # do recruit stuff
                        }
                        elseif(isset($_ POST['disband']))
                        {
                        # do disband stuff
                        }
                        else
                        {
                        # nothing submitted
                        }
                        [/php]
                        I know this is a php forum but do you (markus or anyone else) know how to do the equivalent in javascript?

                        Comment

                        • Markus
                          Recognized Expert Expert
                          • Jun 2007
                          • 6092

                          #13
                          Originally posted by TheServant
                          I know this is a php forum but do you (markus or anyone else) know how to do the equivalent in javascript?
                          I can give it a shot:
                          [code=javascript]
                          function doForm(buttonPr essed)
                          {
                          if(buttonPresse d == "Recruit")
                          {
                          // do something
                          alert(buttonPre ssed);
                          }
                          else // 'disband' was pressed
                          {
                          # do something
                          alert(buttonPre ssed);
                          }
                          }
                          [/code]
                          [code=html]
                          <form name="someForm" method="post" action="page.ph p">
                          <input type="text" name=" ...




                          <input type="submit" name="recruit" onclick="doForm (this.value)" value="recruit" />

                          <!-- you get the idea.. //-->

                          [/code]

                          I'm not expert with javascript though, so maybe you should ask over there?

                          Comment

                          • TheServant
                            Recognized Expert Top Contributor
                            • Feb 2008
                            • 1168

                            #14
                            Originally posted by markusn00b
                            I can give it a shot:
                            [code=javascript]
                            function doForm(buttonPr essed)
                            {
                            if(buttonPresse d == "Recruit")
                            {
                            // do something
                            alert(buttonPre ssed);
                            }
                            else // 'disband' was pressed
                            {
                            # do something
                            alert(buttonPre ssed);
                            }
                            }
                            [/code]
                            [code=html]
                            <form name="someForm" method="post" action="page.ph p">
                            <input type="text" name=" ...




                            <input type="submit" name="recruit" onclick="doForm (this.value)" value="recruit" />

                            <!-- you get the idea.. //-->

                            [/code]

                            I'm not expert with javascript though, so maybe you should ask over there?
                            Sounds good, I will give it a go, but make a post... "OVER THERE"... on the other side (excuse the pun).

                            Comment

                            • rhys
                              New Member
                              • Nov 2006
                              • 25

                              #15
                              Originally posted by dlite922
                              Another way I do it, is the NAME of the buttons are the same but the values are not, so i just get the value of the button and run appropriate action.

                              [PHP]

                              <input type="submit" name="pageActio n" value="Action One" />
                              <input type="submit" name="pageActio n" value="Action Two" />

                              [/PHP]

                              then on php side

                              [PHP]

                              if($_POST['pageAction'] == "Action One")
                              {
                              // do something
                              }
                              else if ($_POST['pageAction'] == "Action Two")
                              {
                              // do something else
                              }

                              [/PHP]

                              Anybody know which way is better? I'll change the way markus is doin it if that's better.
                              You Da Man. Works for me!!!!

                              Comment

                              Working...