p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: SV: Multiple submit buttons


Message #1 by Rodolphe Toots <Rudde@I...> on Wed, 18 Sep 2002 11:06:24 +0200
if i was to do this i would set the type of the buttons to "button" and 
set
onclick action to them specifying where to submit the form
like this:

<input type=3D"button" name=3D"cancel" value=3D"Cancel" 
onclick=3D"cancelAction();">
<input type=3D"button" name=3D"complete" value=3D"Complete"
onclick=3D"completeAction();">

and 2 javascript functions like this:

<script language=3D"javascript">
function cancelAction()
{
	if(confirm("Click OK to cancel this order"))
	{
		document.myForm.action =3D "cancelscript.php"
		document.myForm.submit()
		return true
	}
	else
	{
		alert("text explaining that order is not cancelled")
		return false
	}
}

function completeAction()
{
	validateForm()
	document.myForm.action =3D "completescript.php"
	document.myForm.submit()
	return true
}

function validateForm()
{
	//some form validating code here
}
</script>

-----Ursprungligt meddelande-----
Fr=E5n: Alex Varbola [mailto:a_varbola@h...]
Skickat: den 18 september 2002 03:27
Till: beginning php
=C4mne: [beginning_php] Multiple submit buttons


Hello

I have a form with 2 submit buttons at the end.
Cancel and Complete.
The cancel button cancels a order, closes a session, clearing all 
session
variables. The other button submit validates the form and sends an 
email.

Does php allow multiple submit buttons?
At the moment both buttons do the same thing.

e.g. this does not seem to work

Form
print "<input type=3D\"submit\" name=3D\"cancel\" value=3D\"Cancel\">";
print "<input type=3D\"submit\" name=3D\"complete\" 
value=3D\"Complete\">";

php file

if ($cancel){
    print "goodbye";
}

if ($complete){
    print "Hello";
}

When you click the button do name or value get passed?

Message #2 by "Nikolai Devereaux" <yomama@u...> on Wed, 18 Sep 2002 10:49:50 -0700
> if i was to do this i would set the type of the buttons to
> "button" and set
> onclick action to them specifying where to submit the form
> like this:
   <snip!>


This all depends on preference.  A lot of people don't want to introduce a
lot of javascript into their pages.  The confirmation is a nice feature to
some, but a lot of people don't like javascript popups of any kind (myself
included!)

Also, you're requiring two different PHP scripts to handle the different
actions.  I like to code my scripts such that each form contains all the
code required to process the input for that form... I think things would be
more disorganized if that core form processing logic was spread out among
other files.


Take care,

nik

Message #3 by "David Scott-Bigsby" <DScott-Bigsby@P...> on Wed, 18 Sep 2002 11:15:42 -0700
> Also, you're requiring two different PHP scripts to handle
> the different
> actions.  I like to code my scripts such that each form
> contains all the
> code required to process the input for that form... I think
> things would be
> more disorganized if that core form processing logic was
> spread out among other files.

Has anyone read any good articles on application architecture and PHP?

Nik suggests one model -- for each HTML form a PHP handler (or "brain"), 
which handles multiple actions (e.g., update, cancel).

Another I've seen is one PHP handler for an entire process -- one 
index.php generates multiple pages, handling the submitted output of one 
to generate the next in the workflow.

A third is to have a handler which has no output except a redirect 
header -- its logic drives a switchboard, sending the user to another 
page to show the results.

I'm sure there are other variations...

Is one better than the others? Does it depend on the application?

dsb

***************************************       
David Scott-Bigsby
Product Manager, Web Site and PEDN

PureEdge Solutions
The Leader in Secure XML e-Forms

v:250-708-8145  f:250-708-8010
1-888-517-2675   www.PureEdge.com
***************************************

  Return to Index