Two Submit buttons on one form

I have a basic shopping cart setup, currently the user clicks a button
to remove an item from the cart. The form action posts to itself in
order to update the item list.

Is it possible to have another button in the same form that posts to a
different location?

Please hold your comments if they're not constructive. I am aware of
the existence of Google, it just hasn't worked for me here.

Thanks
ktstone2678 [ Sa, 12 Januar 2008 13:40 ] [ ID #1906259 ]

Re: Two Submit buttons on one form

ktstone2678 [at] gmail.com wrote:
>I have a basic shopping cart setup, currently the user clicks a button
> to remove an item from the cart. The form action posts to itself in
> order to update the item list.
>
> Is it possible to have another button in the same form that posts to a
> different location?
Yes, but you have to use javascript to change the form's action field value.
Paul Lautman [ Sa, 12 Januar 2008 13:56 ] [ ID #1906260 ]

Re: Two Submit buttons on one form

On Sat, 12 Jan 2008 13:40:39 +0100, <ktstone2678 [at] gmail.com> wrote:

> I have a basic shopping cart setup, currently the user clicks a button
> to remove an item from the cart. The form action posts to itself in
> order to update the item list.
>
> Is it possible to have another button in the same form that posts to a
> different location?
>
> Please hold your comments if they're not constructive. I am aware of
> the existence of Google, it just hasn't worked for me here.

Javascript would work, but is unreliable.
Why not post to a page, that on the basis of said button, includes one or
the other script? Or a check at the top of the script that possible
decides to include another one and then exits?
--
Rik Wasmus
luiheidsgoeroe [ Sa, 12 Januar 2008 14:14 ] [ ID #1906263 ]

Re: Two Submit buttons on one form

*** ktstone2678 [at] gmail.com escribió/wrote (Sat, 12 Jan 2008 04:40:39 -0800
(PST)):
> I have a basic shopping cart setup, currently the user clicks a button
> to remove an item from the cart. The form action posts to itself in
> order to update the item list.
>
> Is it possible to have another button in the same form that posts to a
> different location?
>
> Please hold your comments if they're not constructive. I am aware of
> the existence of Google, it just hasn't worked for me here.

I'll personally give buttons a name and make them all submit to self page
(see sample code on bottom). Then, check which name exists and act
accordingly: header('Location: ...') is your friend.

Make sure it's cross browser (IE has some problems if buttons are
type="image") and make sure of what happens when user hits the "enter" key.





<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head><title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

<?

echo '<xmp>$_POST: '; var_dump($_POST); echo '</xmp>';

?>

<form action="" method="post">
<input name="remove" type="submit" value="Remove">
<input name="checkout" type="submit" value="Checkout">
</form>

</body>
</html>


--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor austrohúngaro: http://www.demogracia.com
--
webmasterNOSPAMTHANKS [ Sa, 12 Januar 2008 14:28 ] [ ID #1906264 ]

Re: Two Submit buttons on one form

In article <8285a830-6c14-44bf-ae55-84122934817b [at] t1g2000pra.googlegroups.com>,
<ktstone2678 [at] gmail.com> wrote:
>I have a basic shopping cart setup, currently the user clicks a button
>to remove an item from the cart. The form action posts to itself in
>order to update the item list.
>
>Is it possible to have another button in the same form that posts to a
>different location?

Why would you want to do that? Why not just have the second submit
button post to the same location? Two submit buttons on the same
form can perform two different functions. I do this all the time.
The buttons have different value parameters, the php script picks up
on which one was clicked, and acts accordingly.

If you want two buttons in the same form to post to different
locations, you need javascript to modify the form URL. Not a good
idea, IMO, to rely on javascript when you don't have to.

-A
axlq [ Sa, 12 Januar 2008 19:31 ] [ ID #1906269 ]

Re: Two Submit buttons on one form

On Jan 12, 1:31 pm, a... [at] spamcop.net (axlq) wrote:
> In article <8285a830-6c14-44bf-ae55-841229348... [at] t1g2000pra.googlegroups.com>,
>
> <ktstone2... [at] gmail.com> wrote:
> >I have a basic shopping cart setup, currently the user clicks a button
> >to remove an item from the cart. The form action posts to itself in
> >order to update the item list.
>
> >Is it possible to have another button in the same form that posts to a
> >different location?
>
> Why would you want to do that? Why not just have the second submit
> button post to the same location? Two submit buttons on the same
> form can perform two different functions. I do this all the time.
> The buttons have different value parameters, the php script picks up
> on which one was clicked, and acts accordingly.
>
> If you want two buttons in the same form to post to different
> locations, you need javascript to modify the form URL. Not a good
> idea, IMO, to rely on javascript when you don't have to.
>
> -A

Ideally that's would I would like to do. I need to do research on how
to accomplish that , but I think that's the best solution. Thanks for
you advice everyone
ktstone2678 [ So, 13 Januar 2008 02:20 ] [ ID #1906650 ]

Re: Two Submit buttons on one form

On Jan 12, 8:20 pm, ktstone2... [at] gmail.com wrote:
> On Jan 12, 1:31 pm, a... [at] spamcop.net (axlq) wrote:
>
>
>
> > In article <8285a830-6c14-44bf-ae55-841229348... [at] t1g2000pra.googlegroups.com>,
>
> > <ktstone2... [at] gmail.com> wrote:
> > >I have a basic shopping cart setup, currently the user clicks a button
> > >to remove an item from the cart. The form action posts to itself in
> > >order to update the item list.
>
> > >Is it possible to have another button in the same form that posts to a
> > >different location?
>
> > Why would you want to do that? Why not just have the second submit
> > button post to the same location? Two submit buttons on the same
> > form can perform two different functions. I do this all the time.
> > The buttons have different value parameters, the php script picks up
> > on which one was clicked, and acts accordingly.
>
> > If you want two buttons in the same form to post to different
> > locations, you need javascript to modify the form URL. Not a good
> > idea, IMO, to rely on javascript when you don't have to.
>
> > -A
>
> Ideally that's would I would like to do. I need to do research on how
> to accomplish that , but I think that's the best solution. Thanks for
> you advice everyone

I wanted to post the solution for anyone else who might stumble across
this thread, turns out to be very simple.

<form name="theform" action="oops.htm" method="get">
<input name="one" value="type something here...">
<input type="submit" name="submit" value="first"
onClick="document.theform.action='first.htm';">
<input type="submit" name="submit" value="second"
onClick="document.theform.action='second.htm';">
</form>
ktstone2678 [ So, 13 Januar 2008 02:59 ] [ ID #1906655 ]

Re: Two Submit buttons on one form

On Sun, 13 Jan 2008 02:59:06 +0100, <ktstone2678 [at] gmail.com> wrote:
> On Jan 12, 8:20 pm, ktstone2... [at] gmail.com wrote:
>> On Jan 12, 1:31 pm, a... [at] spamcop.net (axlq) wrote:
> <form name=3D"theform" action=3D"oops.htm" method=3D"get">
> <input name=3D"one" value=3D"type something here...">
> <input type=3D"submit" name=3D"submit" value=3D"first"
> onClick=3D"document.theform.action=3D'first.htm';">
> <input type=3D"submit" name=3D"submit" value=3D"second"
> onClick=3D"document.theform.action=3D'second.htm';">
> </form>

Which, depending on which statistic one believes, would not work for 5% =
- =

15% of your visitors. Also, If you're going to use javascript, ask in =

comp.lang.javascript for best practises.

-- =

Rik Wasmus
luiheidsgoeroe [ So, 13 Januar 2008 03:12 ] [ ID #1906657 ]

Re: Two Submit buttons on one form

axlq wrote:
> If you want two buttons in the same form to post to different
> locations, you need javascript to modify the form URL. Not a good
> idea, IMO, to rely on javascript when you don't have to.

I agree, PHP could handle it, just post the form to a single script which
will redirect to your desired location. In order to do this, I think you
could use CURL to re-submit the form data to the desired location, or just
think twice about it and hu.. I don't know.. ask the user to verify the data
he just entered and re-submit it itself =P. It's always worth it anyway !

---form.html---
<form action="post-redirector.php" method="post">
....
<input type="submit" name="button1" value="go to location 1" />

<input type="submit" name="button2" value="go to location 2" />
<input type="hidden" name="form_sent" value="1" />
</form>

---post-redirector.php---
<?php

# regen_form(): regenerates hidden form from array (usually $_POST)
function regen_form($data, $array_name=null) {
foreach( $data as $field => $value ) {
if( is_array($value) ) {
regen_form($value, $field);
} else {
if( isset($array_name) ) $field = sprintf('%s[%s]',
$array_name, $field);
echo ' <input type="hidden" name="' . $field . '" value="' .
$value . '" />' . "\r\n";
}
}
}

if( isset($_POST['form_sent']) ) {
// stripslashes if hellish_quote_gpc on

// it's useless to sanitize your data here, as someone could always
// bypass the redirector to directly submit a custom form to the
// final script.

if ( isset($_POST['button1']) ) $location = 'location1.php';
elseif( isset($_POST['button2']) ) $location = 'location2.php';
else die('internal error #01 / hijack attempt');

ob_start();
ob_implicit_flush(false);
regen_form($_POST);
$form = ob_get_contents();
ob_clean();
}

?>
<!-- xhtml strict dtd, html header, styles, body tag; that stuff.. -->

<p>
please verify your information:

<!-- print the $_POST here, you might want to use a variant of
regen_form(), write it manually or even lazily print_r() it -->
</p>

<p>
<form action="<?php echo $location; ?>" method="post">
<?php echo $form; ?>
<input type="submit" name="verified" value="it's fine" />
</form>
</p>

<!-- body tag end, html footer, etc. -->
<?php ob_end_flush() ?>


The function is recursive and reproduces form-generated arrays so you don't
miss a thing; but you can just rewrite the form manually as well.

The output buffer is there by (good?) habit, not absolutely needed.


Of course, you can also pass the serialized & encoded $_POST via $_GET,
process it and then directly redirect the user or reload the page without
the $_GET (and thus don't process anything after reload) but I think you
might be limited in the size of the form.


Good luck ^^'.

-thib´
thyb0 [ So, 13 Januar 2008 06:53 ] [ ID #1906696 ]

Re: Two Submit buttons on one form

..oO(ktstone2678 [at] gmail.com)

>I wanted to post the solution for anyone else who might stumble across
>this thread, turns out to be very simple.
>
><form name="theform" action="oops.htm" method="get">
><input name="one" value="type something here...">
><input type="submit" name="submit" value="first"
>onClick="document.theform.action='first.htm';">
><input type="submit" name="submit" value="second"
>onClick="document.theform.action='second.htm';">
></form>

That's no solution. You really don't want to do that.

Do it server-side.

Micha
Michael Fesser [ So, 13 Januar 2008 23:01 ] [ ID #1906758 ]

Re: Two Submit buttons on one form

ktstone2678 [at] gmail.com wrote:
> On Jan 12, 8:20 pm, ktstone2... [at] gmail.com wrote:
>> On Jan 12, 1:31 pm, a... [at] spamcop.net (axlq) wrote:
>>
>>
>>
>>> In article <8285a830-6c14-44bf-ae55-841229348... [at] t1g2000pra.googlegroups.com>,
>>> <ktstone2... [at] gmail.com> wrote:
>>>> I have a basic shopping cart setup, currently the user clicks a button
>>>> to remove an item from the cart. The form action posts to itself in
>>>> order to update the item list.
>>>> Is it possible to have another button in the same form that posts to a
>>>> different location?
>>> Why would you want to do that? Why not just have the second submit
>>> button post to the same location? Two submit buttons on the same
>>> form can perform two different functions. I do this all the time.
>>> The buttons have different value parameters, the php script picks up
>>> on which one was clicked, and acts accordingly.
>>> If you want two buttons in the same form to post to different
>>> locations, you need javascript to modify the form URL. Not a good
>>> idea, IMO, to rely on javascript when you don't have to.
>>> -A
>> Ideally that's would I would like to do. I need to do research on how
>> to accomplish that , but I think that's the best solution. Thanks for
>> you advice everyone
>
> I wanted to post the solution for anyone else who might stumble across
> this thread, turns out to be very simple.
>
> <form name="theform" action="oops.htm" method="get">
> <input name="one" value="type something here...">
> <input type="submit" name="submit" value="first"
> onClick="document.theform.action='first.htm';">
> <input type="submit" name="submit" value="second"
> onClick="document.theform.action='second.htm';">
> </form>
>

Yep, and it fails for me and about 10-15& of the rest of the world
because we browse with javascript disabled.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex [at] attglobal.net
==================
Jerry Stuckle [ Mo, 14 Januar 2008 00:58 ] [ ID #1907453 ]

Re: Two Submit buttons on one form

thib` - excellent post, thanks so much for taking the time to help.
Awesome
ktstone2678 [ Mo, 14 Januar 2008 04:45 ] [ ID #1907482 ]
PHP » comp.lang.php » Two Submit buttons on one form

Vorheriges Thema: Re: has anyone successfully run PHP installer on vista?
Nächstes Thema: Ways to assign a value to a session variable.