遍历$ _POST变量 [英] Looping through $_POST variables

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

问题描述

对不起,我找不到此问题的适当标题. 我使用for循环生成了以下内容,并使用以下模式将了提交按钮的名称进行了串联: Submit_edit_category_1 Submit_edit_category_2 Submit_edit_category_3

Sorry i could not find a proper title to this question. I have generated the following using a for loop and I have concatenated the names of the submits buttons using the pattern below: submit_edit_category_1 submit_edit_category_2 submit_edit_category_3

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

我想遍历这些值,以便可以执行按钮操作edit_category和类别ID为1,2或3.

I want to loop through these values so that I can the button action whichis edit_category and the category id which is 1,2 or 3. I want to so something like:

if(isset($_POST) == 'edit_category'))
{
    //code here
}

有人建议我这样做:

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

这似乎不起作用.有人可以给我一种不同的方式吗? 谢谢!

This does not seem to work..Can anyone give me a different way to do it? Thanks!

推荐答案

这是我要做什么:

对于实际形式,我将使用数组键来传达操作和相关的ID信息.

for the actual form, I'd use array keys to communicate action and relevant id info.

$cat_id =  $obj_categories_admin->categories[$i]['category_id'];

echo "<input type='submit' value = 'Edit' name='submit[edit_category][" . $cat_id . "]'/>";

然后发布后,我可以这样做:

then when posted, I can do:

<?php

list($action, $action_params) = each($_POST['submit']);
list($cat_id, $button_label) = each($action_params);

print_r($_POST['submit']); // prints array('edit_category' => array('1' => 'Edit'))
echo($action); //prints "edit_category"
print_r($action_params); //prints array('1' => 'Edit')
echo($cat_id); //prints "1"
echo($button_label); //prints "Edit"

有关each()的更多信息,请访问: http://us2.php.net/each .我个人一直觉得按钮标签之间没有区别,这是令人沮丧的价值.使用数组键将信息填充到按钮中一直是我最喜欢的技巧.

edit: for more info on each(), go here: http://us2.php.net/each . I've personally always felt that 's lack of differentation between the button label and the it's value to be frustrating. Using an array key to stuff info into the button has always been my favorite hack.

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

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