how to identify if a button is pressed?

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
php12342005
Forum Commoner
Posts: 79
Joined: Mon Mar 21, 2005 3:35 am

how to identify if a button is pressed?

Post by php12342005 »

assume there are 2 forms (form1 and form2), both forms contain button named "send";

if only one form, it is easy to identify if the button is pressed by code:
if(isset($_POST['send']))//works
{
}

but there are 2 buttons with same name in 2 forms, can we identify a button using its parent?
E.g.
if(isset($_POST['window.document.form1.send']))
{
}
code above is not working, I need correct code.

thanks
dyonak
Forum Commoner
Posts: 56
Joined: Wed Jun 22, 2005 10:22 am
Location: Minneapolis, MN
Contact:

Post by dyonak »

You need to set the name parameter for each of your submit buttons on your forms to unique values: foosubmit and barsubmit for example.

Code: Select all

//Form 1 handler
if isset ($_POST['foosubmit']){
    do 'this';
    }

//Form 2 handler
if isset  ($_POST['barsubmit']){
    do 'this';
    }
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

If you want to separate between them, just use a different "name" attribute for each of them..

Code: Select all

<input type='submit' name='send1' Value='Send'/>
<input type='submit' name='send2' Value='Send'/>
Or use a hidden variable to distinguish them

Code: Select all

<form action=&quote;&quote; method=&quote;post&quote;>
<input type='submit' name='send' value='Send'/>
<input type='hidden' name='form' value='1'/>
</form>

<form action=&quote;&quote; method=&quote;post&quote;>
<input type='submit' name='send' value='Send'/>
<input type='hidden' name='form' value='2'/>
</form>
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Use a hidden field in both the forms

Code: Select all

<form...>
<input type=&quote;hidden&quote; name=&quote;hsend&quote; value=&quote;1&quote;/>
...
<input type=&quote;submit&quote; name=&quote;send&quote; value=&quote; Send &quote;/>
</form>
.......
<form...>
<input type=&quote;hidden&quote; name=&quote;hsend&quote; value=&quote;2&quote;/>
...
<input type=&quote;submit&quote; name=&quote;send&quote; value=&quote; Send &quote;/>
</form>
And check for $_POST['hsend']

Believe but in rare situations, the button name never gets sent over GET or POST.
Post Reply