开发者

Php - Which button is clicked

I am writing a script where I provide two buttons "Grant" and "Revoke" to the user. The problem is that both of them are set and "Grant" button always takes precedence.

Below is Sample code

<form name="form_access" id="form_access" action="" method="POST" class="access">
|
|
| 

<input type="hidden" name="accessaction" value="Grant Access"/>
<input typ开发者_开发问答e="hidden" name="revokeaction" value="Revoke Access"/>

<a id="_access_btt" class="button" href="#">Grant Access</a>

    <img class="ajaxload" style="display:none;" id="ajaxld" src="images/ajax-loader.gif"/>
    <a id="_revoke_btt" class="button" href="#">Revoke Access</a>

    <img class="ajaxload" style="display:none;" id="ajaxld1" src="images/ajax-loader.gif"/>
</form>


Rather than using two hidden elements, Why dont you use a single hidden element, something like this?

for example: you could use:

<input type="hidden" name="accessPermission" value=""/>

and depending upon what button user clicks, with the help of javascript you could set this action's value too:

document.form_access.accessPermission.value = "Revoke Access"

and

document.form_access.accessPermission.value = "Grant Access"

and then submit the form!

so in the end, you can just check value $_REQUEST['accessPermission'] in your PHP,

and you will get whatever value you have set for your hidden form element accessPermission:

if its value is Revoke Access you will get $_REQUEST['accessPermission'] as 'Revoke Access'

if its value is Grant Access you will get $_REQUEST['accessPermission'] as 'Grant Access'

So at the end, you can construct your form as:

<form name="form_access" id="form_access" action="" method="POST" class="access">
|
|
| 

<input type="hidden" name="accessPermission" value=""/>

<a id="_access_btt" class="button" href="javascript:document.form_access.accessPermission.value = 'Grant Access'">Grant Access</a>

    <img class="ajaxload" style="display:none;" id="ajaxld" src="images/ajax-loader.gif"/>
    <a id="_revoke_btt" class="button" href="javascript:document.form_access.accessPermission.value = 'Revoke Access'">Revoke Access</a>

    <img class="ajaxload" style="display:none;" id="ajaxld1" src="images/ajax-loader.gif"/>
</form>

And Both the values are set because, its really specifically set by the lines:

<input type="hidden" name="accessaction" value="Grant Access"/>
<input type="hidden" name="revokeaction" value="Revoke Access"/>

Grant Action takes precedence, because that's appears first.


<input type="submit" name="accessaction" value="Grant Access"/>
<input type="submit" name="revokeaction" value="Revoke Access"/>

To check if Grant Access was pressed, you can check isset($_POST['accessaction']).
To check if Revoke Access was pressed, you can check isset($_POST['revokeaction']).

All browsers will only set the button that was pressed, IE is stupid and sets the value to the coordinates or something, other browsers set it to true, and Firefox usually sets the value to the t4ext on the button. But the best way is to just check the $_POST array for which one is present.

EDIT

Using JS to solve this might be okay, but this solution works when users have JS on or off and therefore I think it is more elegant

EDIT

If you do not want to use these as actual buttons, you can set them to display: none then trigger a click on the one you want to actually submit the form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜