循环 $_POST [英] Loop through $_POST

查看:39
本文介绍了循环 $_POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 for 循环,它实际上显示了产品名称和几个按钮,例如:编辑、更新、取消对于我展示的每个产品,它都有自己的一组编辑器、更新和取消按钮,如下所示.

I have a for loop which actually displays a product name and several buttons like: Edit, Update , Cancel For each product i am displaying , it will have its own set of Edir, Update, and Cancel button as below.

画图编辑更新取消

我想遍历按钮,以便对每个类别执行不同的操作.我正在考虑使用 btn_edit1、btn_edit2 之类的东西作为按钮的名称并使用 for 循环.1, 2 是类别 ID.也许我不够清楚.对不起.谁能给我一些建议?

I want to loop through the buttons so that for each category, I can perform a different action. I was thinking about using something like btn_edit1, btn_edit2 for the name of the button and use a for loop. 1, 2 are the category ids. Maybe Im not clear enough. Sorry for that. Can anyone give me some suggestions?

for($i = 0; $i<count($obj_categories_admin->categories);$i++)
{   

            echo "<tr>";

            echo "<td width='1500'>";
            echo "<input type='text' name='name'  size = '30' value='" . $obj_categories_admin->categories[$i]['name'] . "'/>";

            echo "</td>";

            echo "<td width='500'>";

            echo "<input type='submit' value = 'Update details' name='submit_update_category_" . 
            $obj_categories_admin->categories[$i]['category_id'] . "'/>";

            echo "</td>";



            echo "<td width='500'>";

            echo "<input type='submit' value = 'Edit Sub Categories' name='submit_edit_sub_" . 
            $obj_categories_admin->categories[$i]['category_id'] . "'/>";

            echo "</td>";

            echo "<td width='500'>";
            echo "<input type='submit' value = 'Delete' name='submit_delete_category_" . 
            $obj_categories_admin->categories[$i]['category_id'] . "'/>";
            echo "</td>";

            echo "<td width='500'>";

            echo "<input type='submit' value = 'Cancel' name='cancel'" . "'/>" ;

            echo "</td>";

            echo "</tr>";   
    }

我想做类似的事情

foreach($_POST as $key => $value)
{

}

这样当我点击一个按钮时,它会根据 category_id 执行一个操作.

so that when i click on a button it performs an action depending on the category_id.

我已经按照建议尝试了这个:

I have tried this as suggested:

echo "<input type='submit' name='submit[add_category]'" . 
"[" . $obj_categories_admin->categories[$i]['category_id'] . "]". " value='Add' />";

现在在我的课堂上,我有:

Now in my class, i have:

$a1 = $_POST['submit']; 
    $which_action = reset(array_keys($a1)); 
    $which_category = reset(array_keys($a1[$which_action])); 

但是,我收到错误:未定义的索引提交

But, i am getting the error : undefined index submit

推荐答案

我会使用以下模式给出我提交按钮的名称属性:

I would give the name attributes of my submit buttons using following pattern:

name="submit[which_action][which_category]"

例如对于类别 123 的更新"按钮:

For example for your 'Update' button for category 123:

name="submit[update][123]"

当用户单击任何提交按钮时,要确定用户单击了哪个特定按钮,您只需检查 PHP 代码中的 $_POST['submit'] :

When the user clicks any of the submit buttons, to determine which specific button the user has clicked you just need check for $_POST['submit'] in your PHP code:

$a1 = $_POST['submit'];
$which_action = reset(array_keys($a1));
$which_category = reset(array_keys($a1[$which_action]));

这篇关于循环 $_POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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