1 Form, 2 Actions?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark

    1 Form, 2 Actions?

    I have a lengthy form (i.e., many inputs) on my form.php page. What's
    different about this form than many others I've created is that I want
    the user to have a choice of submitting this form data to one of two
    pages, choice1.php or choice2.php, both of which read $_POST data from
    the form.

    Obviously, a 'submit' button will send the user to the page specified by
    the form's 'action=' setting, so I would have to employ some sort of
    trick. But this is where I'm drawing a blank! What combination of PHP,
    Javascript, and HTML would do the trick?

    Thanks!
    Mark
  • ally666

    #2
    Re: 1 Form, 2 Actions?

    simply:

    Have the form post to submitted.php in there save the variables check
    which page it needs to be sent to and use the redirect function to call
    the relevant page.. (may have to make the variables global, or it may
    still pick them up in the session i can't test at the moment.

    Think that should do what you require.

    Comment

    • Treefrog

      #3
      Re: 1 Form, 2 Actions?

      ally666 wrote:[color=blue]
      > simply:
      >
      > Have the form post to submitted.php in there save the variables check
      > which page it needs to be sent to and use the redirect function to call
      > the relevant page..[/color]

      I think by "redirect function" you mean header("Locatio n: ...")? That
      is not a good idea.
      If the form is post, and contains a lot of data, then the redirect wont
      work because it's a "get". You would need to use something like curl,
      to re POST the data.

      (may have to make the variables global, or it may[color=blue]
      > still pick them up in the session i can't test at the moment.[/color]

      eh? No, just use what ever you give to the script. Personally, I never
      use globals, they're a sick idea and to be avoided like the plague.
      [color=blue]
      > Think that should do what you require.[/color]

      It will now ;o)

      Also, you could change the form action using Javascript. Something
      similar to:

      <script language="javas cript">
      function subby_one()
      {
      document.aform. action="paul.ph p";
      }
      function subby_two()
      {
      document.aform. action="robert. php";
      }
      </script>
      <form action="fred.ph p" id="aform">
      <input type="button" name="butt" onclick="subby_ one()">
      <<input type="button" name="butt" onclick="subby_ two()">
      </form>


      type of thing... I'm a "trial by error" type of javascript coder, so
      that may take a few attempts to get right ;o)

      Comment

      • Kimmo Laine

        #4
        Re: 1 Form, 2 Actions?

        "Mark" <Mark.Fenbers@n oaa.gov> wrote in message
        news:e0gjmt$gbg $1@news.nems.no aa.gov...[color=blue]
        >I have a lengthy form (i.e., many inputs) on my form.php page. What's
        >different about this form than many others I've created is that I want the
        >user to have a choice of submitting this form data to one of two pages,
        >choice1.php or choice2.php, both of which read $_POST data from the form.
        >
        > Obviously, a 'submit' button will send the user to the page specified by
        > the form's 'action=' setting, so I would have to employ some sort of
        > trick. But this is where I'm drawing a blank! What combination of PHP,
        > Javascript, and HTML would do the trick?[/color]


        Set the action to page3.php which has only the following:
        <?php
        if(isset($_POST['submit1']))
        include('page1. php');
        else if(isset($_POST['submit2']))
        include('page2. php');
        ?>

        And in the form you naturally have

        <input type="submit" name="submit1" value="1">
        <input type="submit" name="submit2" value="2">

        --
        "En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
        spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)


        Comment

        • Treefrog

          #5
          Re: 1 Form, 2 Actions?

          Kimmo Laine wrote:
          [color=blue]
          > Set the action to page3.php which has only the following:
          > <?php
          > if(isset($_POST['submit1']))
          > include('page1. php');
          > else if(isset($_POST['submit2']))
          > include('page2. php');
          > ?>
          >
          > And in the form you naturally have
          >
          > <input type="submit" name="submit1" value="1">
          > <input type="submit" name="submit2" value="2">[/color]

          Which presumes thes destination "action" is on his server. I'd think
          that just by the fact that he's wanting to do it this way, would
          suggest that the target form is on another box?

          Comment

          • Tony Marston

            #6
            Re: 1 Form, 2 Actions?

            I don't see what the problem is. It is possible to have more than one submit
            button on any form, with each one having a unique name instead of 'submit'.
            So if you have a form with 'buttonA', 'buttonB' and 'buttonC' when the user
            presses one of those buttons that will be the ONLY one to appear in the POST
            array. I have been using this technique for years, so don't tell me that it
            doesn't work.

            --
            Tony Marston

            This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL



            "Mark" <Mark.Fenbers@n oaa.gov> wrote in message
            news:e0gjmt$gbg $1@news.nems.no aa.gov...[color=blue]
            >I have a lengthy form (i.e., many inputs) on my form.php page. What's
            >different about this form than many others I've created is that I want the
            >user to have a choice of submitting this form data to one of two pages,
            >choice1.php or choice2.php, both of which read $_POST data from the form.
            >
            > Obviously, a 'submit' button will send the user to the page specified by
            > the form's 'action=' setting, so I would have to employ some sort of
            > trick. But this is where I'm drawing a blank! What combination of PHP,
            > Javascript, and HTML would do the trick?
            >
            > Thanks!
            > Mark
            >[/color]


            Comment

            • Andy Jeffries

              #7
              Re: 1 Form, 2 Actions?

              On Thu, 30 Mar 2006 20:00:37 +0100, Tony Marston wrote:[color=blue]
              > I don't see what the problem is. It is possible to have more than one
              > submit button on any form, with each one having a unique name instead of
              > 'submit'. So if you have a form with 'buttonA', 'buttonB' and 'buttonC'
              > when the user presses one of those buttons that will be the ONLY one to
              > appear in the POST array. I have been using this technique for years, so
              > don't tell me that it doesn't work.[/color]

              Congratulations ...now tell me, did you actually read the post you're
              responding to?

              He didn't ask about having multiple submit buttons but about having
              multiple form actions (target scripts) depending on which button is
              pressed.

              Re-read and try again...

              Regards,


              Andy


              --
              Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
              http://www.gphpedit.org | PHP editor for Gnome 2
              http://www.andyjeffries.co.uk | Personal site and photos

              Comment

              • Tony Marston

                #8
                Re: 1 Form, 2 Actions?

                The solution is to post to the same form which then stores its data in the
                $_SESSION array before passing control to the actual form identified on one
                of the submit buttons. The next form, which could be one of several, simply
                picks up its data from the $_SESSION array instead of the POST array. This
                can be done with standard PHP code without the need for any fancy and
                dubious tricks with javascript.

                It's not rocket science, just lateral thinking.

                --
                Tony Marston
                This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL


                "Andy Jeffries" <news@andyjeffr ies.co.uk> wrote in message
                news:pan.2006.0 3.30.19.11.17.7 43469@andyjeffr ies.co.uk...[color=blue]
                > On Thu, 30 Mar 2006 20:00:37 +0100, Tony Marston wrote:[color=green]
                >> I don't see what the problem is. It is possible to have more than one
                >> submit button on any form, with each one having a unique name instead of
                >> 'submit'. So if you have a form with 'buttonA', 'buttonB' and 'buttonC'
                >> when the user presses one of those buttons that will be the ONLY one to
                >> appear in the POST array. I have been using this technique for years, so
                >> don't tell me that it doesn't work.[/color]
                >
                > Congratulations ...now tell me, did you actually read the post you're
                > responding to?
                >
                > He didn't ask about having multiple submit buttons but about having
                > multiple form actions (target scripts) depending on which button is
                > pressed.
                >
                > Re-read and try again...
                >
                > Regards,
                >
                >
                > Andy
                >
                >
                > --
                > Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
                > http://www.gphpedit.org | PHP editor for Gnome 2
                > http://www.andyjeffries.co.uk | Personal site and photos
                >[/color]


                Comment

                • Jamie Meyers

                  #9
                  Re: 1 Form, 2 Actions?

                  You can use JavaScript to change the action of the form. Just name the form
                  (with id), and use a x = document.getEle mentById(id); x.action =
                  "blah.php". Use that in accordance to a certain action of the form (like
                  choosing a ratio button).

                  "Mark" <Mark.Fenbers@n oaa.gov> wrote in message
                  news:e0gjmt$gbg $1@news.nems.no aa.gov...[color=blue]
                  >I have a lengthy form (i.e., many inputs) on my form.php page. What's
                  >different about this form than many others I've created is that I want the
                  >user to have a choice of submitting this form data to one of two pages,
                  >choice1.php or choice2.php, both of which read $_POST data from the form.
                  >
                  > Obviously, a 'submit' button will send the user to the page specified by
                  > the form's 'action=' setting, so I would have to employ some sort of
                  > trick. But this is where I'm drawing a blank! What combination of PHP,
                  > Javascript, and HTML would do the trick?
                  >
                  > Thanks!
                  > Mark[/color]


                  Comment

                  • Richard Levasseur

                    #10
                    Re: 1 Form, 2 Actions?

                    Two ways:
                    You can change the form action using javascript (easy and clean,
                    really), or
                    You can read the submit buttons value and proceed accordingly.

                    The first sounds more like what you want.

                    Case 1:
                    <form>
                    <input fields>
                    <input type="submit" name="submit" value="goto page 1"
                    onClick="form.a ction='page1.ph p';"/>
                    <input type="submit" name="submit" value="goto page 2"
                    onClick="form.a ction='page2.ph p';"/>
                    </form>

                    The javascript in the onClick handler of the submit buttons inherits
                    'form' from the current form they are in, avoiding the use of having to
                    do getElementByID( ) or document.formna me.action=blalb a.

                    Case2:

                    <form action="mypage. php">
                    <input fields>
                    <input type="submit" name="submit" value="goto page 1"/>
                    <input type="submit" name="submit" value="goto page 2"/>
                    </form>

                    mypage.php :
                    <? if($submit == 'goto page 1') { process according to page 1's method
                    } else { process according to page 2's method } ?>
                    Obviously this wouldn't work unless the code for both pages could be
                    consolidated into that single page, in which case you would be better
                    off using case 1.

                    Comment

                    Working...