Page 1 of 1

how to identify if a button is pressed?

Posted: Tue Jul 26, 2005 7:23 am
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

Posted: Tue Jul 26, 2005 7:28 am
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';
    }

Posted: Tue Jul 26, 2005 7:30 am
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>

Posted: Tue Jul 26, 2005 7:30 am
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.