View Full Version : PHP, more versitile everyday!
bcarl314
06-27-2002, 11:28 AM
I just discovered this little how-to-do in php that I though I would share.
I didn't know that you could have multiple submit buttons in a form and determine which one was clicked on in php. Example:
<html>
<body>
<form action="myPhp.php" method="post">
<input type="text" name="fName">
<input type="submit" name="update" value="Update Name">
<input type="submit" name="delete" value="Remove My Name">
</form>
</body>
</html>
and the php file goes like this...
<?php
if(isset($update)) {
print "You're name has been updated";
//do other stuff
}
else if(isset($delete)) {
print "You have been removed";
//do other something
}
else {
print "Huh, I didn't get that? What do you want to do???";
//default something
}
?>
I'm sure you php gurus knew this, but I just discovered it last night in a trial and error frenzy (ie debugging) session.
Spookster
06-27-2002, 01:27 PM
I knew you can detect the name of the button pushed but I hadn't thought of using it in that way. Nice idea. Thanks for sharing. :thumbsup:
Jeewhizz
06-27-2002, 02:09 PM
You can indeed. Just another little thing yo might find useful...
if($delete) is the same as if(isset($delete))
Jee
firepages
06-27-2002, 02:56 PM
... but sadly (IMO) you need to get used to ..
$_POST[delete];
or
$_HTTP_POST_VARS[delete];
best start now as soon enough you will have no choice ;(
SYP}{ER
06-27-2002, 08:37 PM
Is that what they have in store for the next PHP?! Geez, that'll be a huge pain for a whole lot of people. Or no one will update.
Fitzbean
06-28-2002, 12:21 PM
I hear ya - that's silly in my opinion.
You can also have multiple buttons with the same name, and determine the action by the value.. like..
<html>
<body>
<form action="myPhp.php" method="post">
<input type="text" name="fName">
<input type="submit" name="action" value="Update">
<input type="submit" name="action" value="Cancel">
<input type="submit" name="action" value="Remove">
</form>
</body>
</html>
<?
if($action == 'Update') {
print "You're name has been updated";
//do other stuff
}
else if($action == 'Delete') {
print "You have been removed";
//do other something
}
else {
print "Canceling....";
//default something
}
?>
ClubCosmic
07-03-2004, 01:09 PM
this is a good post!
i also have used that method but with checkboxes instead. i think i'll use buttons instead because it looks nicer on a form. its a good idea to always include an else statement at the end in case neither button is selected.
IYou can also have multiple buttons with the same name, and determine the action by the value
This is what i always do, because in a multi-purpose page, you then only need to check if for instance $_POST['action'] is set and if not, go directly to your formbuilding. If set, then i use a switch like
switch ($_POST['action']){
case 'delete':
...
break;
case 'update':
...
break;
default:
$info = 'No action selected. Form is reloaded.';
}
to determine which part of the formprocessing needs to be exectued.
Nightfire
07-04-2004, 11:09 AM
Heh, this is an old thread, almost as old as le spook :p
carl_mcdade
07-04-2004, 03:51 PM
Because PHP programmers can't get it through threir heads to use $_Request a lot of ISPs are going with the default of register_globals on again. :mad:
It is really stupid and annoying to see that they have upgraded PHP consistently and then suddenly register_globals is on again. But this guy has the right solution to getting it straight before you roll out your code to multiple sites like I did.
http://martin.f2o.org/php/portable
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.