确定用于通过PHP提交的按钮 [英] Determining Button used to Submit with PHP

查看:57
本文介绍了确定用于通过PHP提交的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个管理引擎,该引擎需要先管理部分,然后再管理部分中的页面.

出于可用性的考虑,已决定将显示顺序字段显示在列表页面上,这导致需要使用一种格式来包围这些部分以及每个页面组中的一个.

最明显的解决方案是嵌套表单,而xhtml和html5标准都出于非常明显的原因而阻止了它们.这样,我现在将整个页面作为一个表单进行处理,然后根据所提交的按钮来确定要使用的数据.

问题在于按钮具有基于部分ID生成的动态名称.

<button type="submit" class="btn bg-blue2 btn-gradient btn-xs" name="order:pages:<?=$GAS['uid'];?>"><span class="glyphicons glyphicons-roundabout"></span> </button>

按钮的名称是使用其父节" ID为每组页面生成的.这样,我只能选择具有特定前缀的文本字段并处理该数据.

我的问题是,如何检测使用哪个按钮在处理页面上提交表单.我已经考虑过使用var_dump($_POST)substr(),但是不知道/不知道如何正确使用它.

最终结果应该允许我获取所使用按钮的名称(order:pages:id),然后使用substr对其进行分隔,以便我可以使用id来进一步确定文本字段.

一如既往地感谢您.

解决方案

我建议您将按钮的名称保持不变,例如name="edit_btn".

然后,您只需将ID放入button的value属性中,并在PHP端使用$_POST['edit_btn']进行读取.

html:

<button type="submit" value="<?=$ID;?>" name="edit_btn">Edit</button>

PHP:

var_dump($_POST['edit_btn']);

如果您出于某种原因坚持将ID保留在NAME中,则可以执行以下操作:

HTML:

<button name="edit_btn[123]" value="1">Edit</button>

PHP:

var_dump(key($_POST['edit_btn']));

I am creating a management engine that needs to manage sections and then pages within the sections.

For usability it has been decided that the display order fields will be displayed on the listing page, this results in the need to have a form that surrounds the sections as well as one for each of the groups of pages.

The obvious solution was nesting forms, which xhtml and html5 standards both prevent for pretty obvious reasons. As such I am now processing the entire page as a single form and then deciding what data to use based on the button pressed to submit the data.

The problem with this is that the buttons have dynamic names that generate based on the ID of the section.

<button type="submit" class="btn bg-blue2 btn-gradient btn-xs" name="order:pages:<?=$GAS['uid'];?>"><span class="glyphicons glyphicons-roundabout"></span> </button>

The name of the button is generated for each group of pages using it's parent 'section' ID. By doing this I can only select textfields that have a certain prefix and process that data.

My question is, how can I detect which button is used to submit the form on the processing page. I have considered the use of var_dump($_POST) and substr() but don't know / understand how to correctly approach it.

The final result should allow me to get the name of the button used (order:pages:id) and then seperate it using substr so I can use id to further determine textfields.

Thank you as always.

解决方案

Instead of having a different name attribute for your buttons, i suggest that you keep the name the same eg name="edit_btn".

Then you can simply put the ID in the buttons value attribute and read it with $_POST['edit_btn'] on the PHP side.

html:

<button type="submit" value="<?=$ID;?>" name="edit_btn">Edit</button>

PHP:

var_dump($_POST['edit_btn']);

If you for some reason INSIST on keeping the ID in the NAME, you COULD do something like this instead:

HTML:

<button name="edit_btn[123]" value="1">Edit</button>

PHP:

var_dump(key($_POST['edit_btn']));

这篇关于确定用于通过PHP提交的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆