Detect which button is clicked in a form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
morris520
Forum Commoner
Posts: 60
Joined: Thu Sep 18, 2008 8:56 pm
Location: Manchester UK

Detect which button is clicked in a form

Post by morris520 »

Hi guys

I am trying to perform a function like this:

I have used php to create "delete" button with each row of record, though I am not sure whether I am doing the right thing.

Then it sends the form to another php page to process the query. My question is on that page, how can know which delete button of which record is clicked ?

any way to do that?

thanks
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: Detect which button is clicked in a form

Post by thinsoldier »

I'd help but... this isn't hard.
Go find a tutorial.

All I'll say is

Code: Select all

 
<pre>
<?php
print_r($_POST);
?>
</pre>
 
Warning: I have no idea what I'm talking about.
User avatar
morris520
Forum Commoner
Posts: 60
Joined: Thu Sep 18, 2008 8:56 pm
Location: Manchester UK

Re: Detect which button is clicked in a form

Post by morris520 »

thinsoldier wrote:I'd help but... this isn't hard.
Go find a tutorial.

All I'll say is

Code: Select all

 
<pre>
<?php
print_r($_POST);
?>
</pre>
 
is this is right way to do it?

I used a link

Code: Select all

<a href='register.php?id={$_POST}'>Add</a>
This works but it's just a link, but I prefer a button

Thanks for help
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: Detect which button is clicked in a form

Post by pcoder »

You can pass the id in each row of record.Like:

Code: Select all

<a href='deletePage.php?id=ID'>Delete</a>
Where ID uniquely identifies the record.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Detect which button is clicked in a form

Post by Stryks »

If you want to get a clearer idea of what is going on, set up a page with something like the following ...

Code: Select all

 
<?php
   if($_SERVER['REQUEST_METHOD'] == 'POST') {
      print_r($_POST);
?>
<pre>
It would appear you chose to delete item number <?php echo key($_POST['delete']); ?>
</pre>
<?php
   }
?>
 
<form method="post" action="this.php">
   <input type="submit" name="delete[1]" value="Delete">
   <input type="submit" name="delete[2]" value="Delete">
   <input type="submit" name="delete[3]" value="Delete">
</form>
 
Give it a run and you should see the solution you need to implement. If you need more help, feel free to ask.

Hope this helps. :)

[EDIT] - makes more sense when the buttons have different indexes. :oops:
Last edited by Stryks on Thu Sep 18, 2008 11:17 pm, edited 1 time in total.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Detect which button is clicked in a form

Post by Stryks »

The method shown by pcoder is totally valid also. It's just my preference not to use POST and GET together where possible.

But it's all valid so it's all good.

8)
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Detect which button is clicked in a form

Post by papa »

morris520 wrote:Hi guys

I am trying to perform a function like this:

I have used php to create "delete" button with each row of record, though I am not sure whether I am doing the right thing.

Then it sends the form to another php page to process the query. My question is on that page, how can know which delete button of which record is clicked ?

any way to do that?

thanks
I would use checkbuttons and then one submit button. It's then easy to add a "check all" button for more usability.
User avatar
morris520
Forum Commoner
Posts: 60
Joined: Thu Sep 18, 2008 8:56 pm
Location: Manchester UK

Re: Detect which button is clicked in a form

Post by morris520 »

Stryks wrote:If you want to get a clearer idea of what is going on, set up a page with something like the following ...

Code: Select all

 
<?php
   if($_SERVER['REQUEST_METHOD'] == 'POST') {
      print_r($_POST);
?>
<pre>
It would appear you chose to delete item number <?php echo key($_POST['delete']); ?>
</pre>
<?php
   }
?>
 
 
<form method="post" action="this.php">
   <input type="submit" name="delete[1]" value="Delete">
   <input type="submit" name="delete[2]" value="Delete">
   <input type="submit" name="delete[3]" value="Delete">
</form>
 
Give it a run and you should see the solution you need to implement. If you need more help, feel free to ask.

Hope this helps. :)

[EDIT] - makes more sense when the buttons have different indexes. :oops:

Yep.. This works perfectly. This is excatly what I try to do.
Also, the get method is more easier to understand.

Guys, thanks for all of your help
Post Reply