Professional Web Applications Themes

[PHP] multiple FORMS on same page problem. - PHP Development

I am creating a page with 2 buttons. One which will refresh the page and the other that will go to another page. My problem that I need to put all the values into $_POST and have access to when any of the 2 buttons are clicked. If I use the button in 2 seperate form tags I don't have access to all the values in $_POST. Please look at the code below: file (page4.php): <html> <form action="page4.php" method=post> <?php echo "this is page 4"; ?> <input type=text name='someVal' value=''"> <input type='submit' value='page4 Submit'> </form> <form action="page5.php" method=post> <input type='submit' ...

  1. #1

    Default [PHP] multiple FORMS on same page problem.

    I am creating a page with 2 buttons. One which will refresh the page and the other that will go to another page. My problem that I need to put all the values into $_POST and have access to when any of the 2 buttons are clicked.
    If I use the button in 2 seperate form tags I don't have access to all the values in $_POST. Please look at the code below:

    file (page4.php):

    <html>

    <form action="page4.php" method=post>
    <?php echo "this is page 4"; ?>
    <input type=text name='someVal' value=''">
    <input type='submit' value='page4 Submit'>
    </form>
    <form action="page5.php" method=post>
    <input type='submit' value='page5 Submit'>
    </form>
    </html>

    file(page5.php):

    <?php
    $val = $_POST['someVal'];
    echo $val;
    echo "this is page 5";
    ?>

    I would like to see the variable 'someVal' in the $_POST of both "page4 Submit" button and "page5 Submit" button. This code is a simplified version of the more complecated code where a tonnes of other values are involved. Any help is appreciated.

    Moiz
    Moiz M Golawala Guest

  2. #2

    Default RE: [PHP] multiple FORMS on same page problem.

    I don't know of any rule that says you cannot have two submit button in the same form.

    Of course, if you need to know which was clicked, you will either need to name them differently or detect their values in your form processing routine. If the button is clicked that should take you to another form simply do a redirect (after processing all $_POST information);

    header("Location: new page");
    exit; // I always use this because processing continues after header

    good luck,

    Warren Vail

    -----Original Message-----
    From: Golawala, Moiz M (IndSys, GE Interlogix)
    [mailto:Moiz.Golawalage.com]
    Sent: Monday, September 15, 2003 1:17 PM
    To: [email]php-generallists.php.net[/email]
    Subject: [PHP] multiple FORMS on same page problem.


    I am creating a page with 2 buttons. One which will refresh the page and the other that will go to another page. My problem that I need to put all the values into $_POST and have access to when any of the 2 buttons are clicked.
    If I use the button in 2 seperate form tags I don't have access to all the values in $_POST. Please look at the code below:

    file (page4.php):

    <html>

    <form action="page4.php" method=post>
    <?php echo "this is page 4"; ?>
    <input type=text name='someVal' value=''">
    <input type='submit' value='page4 Submit'>
    </form>
    <form action="page5.php" method=post>
    <input type='submit' value='page5 Submit'>
    </form>
    </html>

    file(page5.php):

    <?php
    $val = $_POST['someVal'];
    echo $val;
    echo "this is page 5";
    ?>

    I would like to see the variable 'someVal' in the $_POST of both "page4 Submit" button and "page5 Submit" button. This code is a simplified version of the more complecated code where a tonnes of other values are involved. Any help is appreciated.

    Moiz

    --
    PHP General Mailing List (http://www.php.net/)
    To unsubscribe, visit: http://www.php.net/unsub.php
    Warren Vail Guest

  3. #3

    Default Re: [PHP] multiple FORMS on same page problem.

    You can only use one form, as you've figured out. You can have PHP determine
    the action for each button, though..

    In a form with two buttons, say names "submit1" and "submit2", only one
    variable will be set when the form is submitted, the actual button that was
    clicked. So you can use logic like this in PHP.

    <?php
    if(isset($_REQUEST['submit1']))
    { echo "button 1 was clicked, act accordingly"; }
    elseif(isset($_REQUEST['submit2']))
    { echo "button 2 was clicked, act accordingly"; }
    else
    { echo "no button clicked, do not act accordingly?"; }

    So this would be in "page4.php". $_REQUEST['someVar'] would be available
    here.

    Now, you want to redirect to "page5.php" if button two is pressed, right,
    yet retain the value of $_POST?

    if(isset($_REQUEST['submit2']))
    {
    $_SESSION['post'] = $_POST;
    header("Location: http://www.yourdomain.com/page5.php");
    }

    Now, on "page5.php", to get $_POST back, just do:

    $_POST = $_SESSION['post'];

    That's assuming you have session_start() on each page and you don't have
    anything in $_POST already on "page5.php" that you want to retain.

    ---John Holmes

    PS: Sorry for the top-post, but OExpress s. :)


    ----- Original Message -----
    From: "Golawala, Moiz M (IndSys, GE Interlogix)" <Moiz.Golawalage.com>
    To: <php-generallists.php.net>
    Sent: Monday, September 15, 2003 4:17 PM
    Subject: [PHP] multiple FORMS on same page problem.


    I am creating a page with 2 buttons. One which will refresh the page and the
    other that will go to another page. My problem that I need to put all the
    values into $_POST and have access to when any of the 2 buttons are clicked.
    If I use the button in 2 seperate form tags I don't have access to all the
    values in $_POST. Please look at the code below:

    file (page4.php):

    <html>

    <form action="page4.php" method=post>
    <?php echo "this is page 4"; ?>
    <input type=text name='someVal' value=''">
    <input type='submit' value='page4 Submit'>
    </form>
    <form action="page5.php" method=post>
    <input type='submit' value='page5 Submit'>
    </form>
    </html>

    file(page5.php):

    <?php
    $val = $_POST['someVal'];
    echo $val;
    echo "this is page 5";
    ?>

    I would like to see the variable 'someVal' in the $_POST of both "page4
    Submit" button and "page5 Submit" button. This code is a simplified version
    of the more complecated code where a tonnes of other values are involved.
    Any help is appreciated.

    Moiz

    --
    PHP General Mailing List (http://www.php.net/)
    To unsubscribe, visit: http://www.php.net/unsub.php
    Cpt John W. Holmes Guest

  4. #4

    Default Re: [PHP] multiple FORMS on same page problem.

    --- "Golawala, Moiz M (IndSys, GE Interlogix)" <Moiz.Golawalage.com> wrote:
    > I am creating a page with 2 buttons.
    ....
    > <form action="page4.php" method=post>
    > <?php echo "this is page 4"; ?>
    > <input type=text name='someVal' value=''">
    > <input type='submit' value='page4 Submit'>
    > </form>
    > <form action="page5.php" method=post>
    > <input type='submit' value='page5 Submit'>
    > </form>
    ....
    > I would like to see the variable 'someVal' in the $_POST of both
    > "page4 Submit" button and "page5 Submit" button.
    Then put someVal in both forms. When you submit a form, only the data in the
    form will be sent to the server.

    Chris

    =====
    Become a better Web developer with the HTTP Developer's Handbook
    [url]http://httphandbook.org/[/url]
    Chris Shiflett Guest

  5. #5

    Default Re: [PHP] multiple FORMS on same page problem.

    On Mon, 15 Sep 2003 15:35:50 -0500, Cpt John W. Holmes created an
    award-winning crop circle <010901c37bc8$f3bd2200$a629089bTBHHCCDR>,
    which, when translated into English, means this:
    > [...]
    >
    > PS: Sorry for the top-post, but OExpress s. :)
    >
    > [...]
    I believe that there is a patch for MSOE that fixes
    its quoting problem. Google for "quotefix" and/or "sigfix."

    Gary Petersen Guest

  6. #6

    Default RE: [PHP] multiple FORMS on same page problem.

    It is almost working.. I can't figure out why I can get the "someVal" to page5.php.

    file: page4.php

    <?php

    if (isset($_REQUEST['submit1'])){
    echo "button 1 was clicked, act accordingly";
    echo "this the request values";
    echo $_REQUEST['someVal'];
    }elseif(isset($_REQUEST['submit2'])){
    session_start();
    echo "button 2 was clicked, act accordingly";
    $_SESSION['post'] = $_POST;
    header("Location: http://127.0.0.1/Alarms/page5.php?<?=SID?>");
    }else{
    echo "NO button clicked, do not act accordingly";
    }
    ?>
    <html>
    <form action="page4.php" method=post>
    <?php echo "this is page 4"; ?>
    <input type=text name='someVal' value=''>
    <input type='submit' name="submit1" value='page4 Submit'>
    </form>


    <form action="page4.php" method=post>
    <input type='submit' name="submit2" value='page5 Submit'>
    </form>
    </html>


    file: page5.php
    <?php
    $_POST = $_SESSION['post'];
    $val = $_POST['someVal'];
    echo $val;
    echo "this is page 5";
    ?>

    for some reason I get the error that the header was already sent. I have to send the session Id to the next page the old fashioned way since I have another problem that causes a new session to be created everytime a page is loaded. Do you where the problem is?

    Thanx
    Moiz

    -----Original Message-----
    From: CPT John W. Holmes [mailto:holmes072000charter.net]
    Sent: Monday, September 15, 2003 4:36 PM
    To: Golawala, Moiz M (IndSys, GE Interlogix); [email]php-generallists.php.net[/email]
    Subject: Re: [PHP] multiple FORMS on same page problem.


    You can only use one form, as you've figured out. You can have PHP determine
    the action for each button, though..

    In a form with two buttons, say names "submit1" and "submit2", only one
    variable will be set when the form is submitted, the actual button that was
    clicked. So you can use logic like this in PHP.

    <?php
    if(isset($_REQUEST['submit1']))
    { echo "button 1 was clicked, act accordingly"; }
    elseif(isset($_REQUEST['submit2']))
    { echo "button 2 was clicked, act accordingly"; }
    else
    { echo "no button clicked, do not act accordingly?"; }

    So this would be in "page4.php". $_REQUEST['someVar'] would be available
    here.

    Now, you want to redirect to "page5.php" if button two is pressed, right,
    yet retain the value of $_POST?

    if(isset($_REQUEST['submit2']))
    {
    $_SESSION['post'] = $_POST;
    header("Location: http://www.yourdomain.com/page5.php");
    }

    Now, on "page5.php", to get $_POST back, just do:

    $_POST = $_SESSION['post'];

    That's assuming you have session_start() on each page and you don't have
    anything in $_POST already on "page5.php" that you want to retain.

    ---John Holmes

    PS: Sorry for the top-post, but OExpress s. :)


    ----- Original Message -----
    From: "Golawala, Moiz M (IndSys, GE Interlogix)" <Moiz.Golawalage.com>
    To: <php-generallists.php.net>
    Sent: Monday, September 15, 2003 4:17 PM
    Subject: [PHP] multiple FORMS on same page problem.


    I am creating a page with 2 buttons. One which will refresh the page and the
    other that will go to another page. My problem that I need to put all the
    values into $_POST and have access to when any of the 2 buttons are clicked.
    If I use the button in 2 seperate form tags I don't have access to all the
    values in $_POST. Please look at the code below:

    file (page4.php):

    <html>

    <form action="page4.php" method=post>
    <?php echo "this is page 4"; ?>
    <input type=text name='someVal' value=''">
    <input type='submit' value='page4 Submit'>
    </form>
    <form action="page5.php" method=post>
    <input type='submit' value='page5 Submit'>
    </form>
    </html>

    file(page5.php):

    <?php
    $val = $_POST['someVal'];
    echo $val;
    echo "this is page 5";
    ?>

    I would like to see the variable 'someVal' in the $_POST of both "page4
    Submit" button and "page5 Submit" button. This code is a simplified version
    of the more complecated code where a tonnes of other values are involved.
    Any help is appreciated.

    Moiz

    --
    PHP General Mailing List (http://www.php.net/)
    To unsubscribe, visit: http://www.php.net/unsub.php

    --
    PHP General Mailing List (http://www.php.net/)
    To unsubscribe, visit: http://www.php.net/unsub.php
    Moiz M Golawala Guest

  7. #7

    Default RE: [PHP] multiple FORMS on same page problem.

    You could also redirect with javascript:

    echo "<Script Lang=javascript>";
    echo "window.location.href = 'page5.php'";
    echo "</script>";

    Grtz
    Jan

    -----Original Message-----
    From: John W. Holmes [mailto:holmes072000charter.net]
    Sent: Dienstag, 16. September 2003 18:06
    To: Golawala, Moiz M (IndSys, GE Interlogix)
    Cc: [email]php-generallists.php.net[/email]
    Subject: Re: [PHP] multiple FORMS on same page problem.


    Golawala, Moiz M (IndSys, GE Interlogix) wrote:
    > It is almost working.. I can't figure out why I can get the "someVal" to page5.php.
    >
    > file: page4.php
    >
    > <?php
    >
    > if (isset($_REQUEST['submit1'])){
    > echo "button 1 was clicked, act accordingly";
    > echo "this the request values";
    > echo $_REQUEST['someVal'];
    > }elseif(isset($_REQUEST['submit2'])){
    > session_start();
    > echo "button 2 was clicked, act accordingly";
    > $_SESSION['post'] = $_POST;
    > header("Location: http://127.0.0.1/Alarms/page5.php?<?=SID?>");
    header("Location: http://127.0.0.1/Alarms/page5.php?" . SID);

    But this is not going to work. You can only redirect with header() if
    there has not been any output. That's why you are getting the errors.

    --
    ---John Holmes...

    Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/

    php|architect: The Magazine for PHP Professionals www.phparch.com

    --
    PHP General Mailing List (http://www.php.net/)
    To unsubscribe, visit: http://www.php.net/unsub.php
    Jan Ruessel Guest

Similar Threads

  1. Replies: 4
    Last Post: September 18th, 11:58 PM
  2. Multiple Forms in one page
    By mohadi in forum Coldfusion - Getting Started
    Replies: 6
    Last Post: September 30th, 02:46 PM
  3. Multiple forms on one page
    By steel_quill in forum Dreamweaver AppDev
    Replies: 1
    Last Post: February 23rd, 08:11 PM
  4. How to include multiple <forms> on one ASP.NET page
    By Ben Turner in forum ASP.NET General
    Replies: 2
    Last Post: July 31st, 02:00 PM
  5. Replies: 1
    Last Post: July 29th, 11:05 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139