Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-27-2002, 11:28 AM   PM User | #1
bcarl314
Mega-ultimate member


 
Join Date: Jun 2002
Location: Winona, MN - The land of 10,000 lakes
Posts: 1,855
Thanks: 1
Thanked 45 Times in 42 Posts
bcarl314 will become famous soon enough
PHP, more versitile everyday!

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:
Code:
<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...

Code:
<?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.
bcarl314 is offline   Reply With Quote
Old 06-27-2002, 01:27 PM   PM User | #2
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,220
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
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.
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Old 06-27-2002, 02:09 PM   PM User | #3
Jeewhizz
Regular Coder


 
Join Date: May 2002
Location: London, England
Posts: 369
Thanks: 0
Thanked 0 Times in 0 Posts
Jeewhizz is an unknown quantity at this point
You can indeed. Just another little thing yo might find useful...

if($delete) is the same as if(isset($delete))

Jee
__________________
Jeewhizz - MySQL Moderator
http://www.sitehq.co.uk
PHP and MySQL Hosting
Jeewhizz is offline   Reply With Quote
Old 06-27-2002, 02:56 PM   PM User | #4
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,901
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
... 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 ;(
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 06-27-2002, 08:37 PM   PM User | #5
SYP}{ER
Regular Coder

 
Join Date: Jun 2002
Location: Ontario, Canada
Posts: 183
Thanks: 0
Thanked 0 Times in 0 Posts
SYP}{ER is an unknown quantity at this point
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.
__________________
Offtone.com - In the works...
SYP}{ER is offline   Reply With Quote
Old 06-28-2002, 12:21 PM   PM User | #6
Fitzbean
New Coder

 
Join Date: Jun 2002
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Fitzbean is an unknown quantity at this point
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
}
?>
Fitzbean is offline   Reply With Quote
Old 07-03-2004, 01:09 PM   PM User | #7
ClubCosmic
Regular Coder

 
Join Date: May 2004
Posts: 144
Thanks: 0
Thanked 0 Times in 0 Posts
ClubCosmic is an unknown quantity at this point
good use of buttons

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.
ClubCosmic is offline   Reply With Quote
Old 07-04-2004, 08:42 AM   PM User | #8
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
Quote:
Originally Posted by Fitzbean
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.
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 07-04-2004, 11:09 AM   PM User | #9
Nightfire
Senior Coder

 
Nightfire's Avatar
 
Join Date: Jun 2002
Posts: 4,266
Thanks: 6
Thanked 48 Times in 48 Posts
Nightfire is on a distinguished road
Heh, this is an old thread, almost as old as le spook
__________________
Blue Panda
Website Design | 1 Pound Ads | 'ow much? | Coding Geeks
Nightfire is offline   Reply With Quote
Old 07-04-2004, 03:51 PM   PM User | #10
carl_mcdade
Regular Coder

 
Join Date: May 2004
Location: sweden
Posts: 236
Thanks: 0
Thanked 0 Times in 0 Posts
carl_mcdade is an unknown quantity at this point
The world has gone mad

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.

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
__________________
Carl McDade
_____________
Hiveminds Magazine
for web publisher and community builders
eRuby Tutorials
carl_mcdade is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:09 PM.


Advertisement
Log in to turn off these ads.