Page 1 of 1

Detect which button is clicked in a form

Posted: Thu Sep 18, 2008 9:01 pm
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

Re: Detect which button is clicked in a form

Posted: Thu Sep 18, 2008 10:31 pm
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>
 

Re: Detect which button is clicked in a form

Posted: Thu Sep 18, 2008 10:43 pm
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

Re: Detect which button is clicked in a form

Posted: Thu Sep 18, 2008 11:08 pm
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.

Re: Detect which button is clicked in a form

Posted: Thu Sep 18, 2008 11:14 pm
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:

Re: Detect which button is clicked in a form

Posted: Thu Sep 18, 2008 11:16 pm
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)

Re: Detect which button is clicked in a form

Posted: Fri Sep 19, 2008 4:35 am
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.

Re: Detect which button is clicked in a form

Posted: Fri Sep 19, 2008 9:29 am
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