p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Multiple submit buttons


Message #1 by "Alex Varbola" <a_varbola@h...> on Wed, 18 Sep 2002 03:26:33
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=\"submit\" name=\"cancel\" value=\"Cancel\">";
print "<input type=\"submit\" name=\"complete\" value=\"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 Tue, 17 Sep 2002 19:35:35 -0700
When you click a submit button, both the name AND value get passed.

For example:

-------------------------------------
<?php

if(isset($_POST))
{
   echo "<PRE>\$_POST is: ";
   print_r($_POST);
   echo "</PRE>\n";
}
?>
<HR />
<FORM method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
  <INPUT type="submit" name="cancel" value="Cancel!!" />
  <INPUT type="submit" name="gretel" value="huh??"    />
</FORM>
-------------------------------------


clicking on "Cancel!!" will result in the following $_POST array:

$_POST is: Array
(
    [cancel] => Cancel!!
)

Similarly, clicking on "huh??" will result in the following $_POST array:

$_POST is: Array
(
    [gretel] => huh??
)


See what's going on?  As with any form input field, the NAME of the input
field is the index into the form array (or the name of the global variable,
if register_globals = on), and the VALUE of the field is assigned to that
index/variable.


Take care,

nik

Message #3 by "Tonsil" <tonsil@s...> on Tue, 17 Sep 2002 22:44:52 -0400
Otter work, I did a test in IE with the following:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE>test</TITLE>
</HEAD>

<BODY>

<?php
if ($_POST["cancel"]){
    print "goodbye";
}

else if ($_POST["complete"]){
    print "Hello";
}
else {
?>
<form action="./test.php" method="post">
<?php 
print "<input type=\"submit\" name=\"cancel\" value=\"Cancel\">";
print "<input type=\"submit\" name=\"complete\" 
value=\"Complete\">";
} 
?>
</form>
</BODY>
</HTML>

Make sure you're taking care of the whole REGISTER_GLOBALS bit. 
Something else to consider might be putting the submit buttons in 
two different forms. If you click the "Cancel" button, from how I 
understand it, you don't really need to send the information in the 
form anyway. 



On 18 Sep 02, at 3:26, Alex Varbola wrote:

> 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=\"submit\" name=\"cancel\" value=\"Cancel\">";
> print "<input type=\"submit\" name=\"complete\" value=\"Complete\">";
>  
> php file
> 
> if ($cancel){
>     print "goodbye";
> }
> 
> if ($complete){
>     print "Hello";
> }
> 
> When you click the button do name or value get passed?
> 
> 



_________________________________________________
This email has been brought to you by:
     House Arrest Daycare (tm)
"Hey, I figgered I wasn't goin' nowhere nohow..."
Message #4 by "Alex Varbola" <a_varbola@h...> on Wed, 18 Sep 2002 23:06:53 +1000
Cheers  Nik

  Return to Index