从多个复选框行收集$ _POST

时间:2012-02-09 22:15:25

标签: php arrays foreach-loop-container

我有一个包含多行复选框的表单,每个复选框都有一个特定的ID,使用foreach循环显示。

你如何从这样的东西中获取$_POST信息?我认为它就像这样$_POST[][],就像一个子数组,但我无法弄清楚如何设置它:

foreach($stakholderArray as $currentEntry) {
    print "<tr class='$bgcolor'>";
    print "<td class='left'>$currentEntry[org]</td>";

    if($currentEntry['dataFound']) {  
        //if data was found for current stakeholder, display it
        print ($currentEntry['Partner']) ? '<td><input type ="checkbox" checked ="checked" /></td>' : '<td><input type ="checkbox"  /></td>';
        print ($currentEntry['Agreement']) ? '<td><input type ="checkbox" checked ="checked" /></td>' : '<td><input type ="checkbox"  /></td>';
        print ($currentEntry['Train']) ? '<td><input type ="checkbox" checked ="checked" /></td>'  : '<td><input type ="checkbox"  /></td>';
        print ($currentEntry['Meet'])  ? '<td><input type ="checkbox" checked ="checked" /></td>' : '<td><input type ="checkbox"  /></td>';
    }
    else {  //else...no stakeholder data, display empty columns
        print "<td><input type ='checkbox'/></td><td><input type ='checkbox'/></td><td><input type ='checkbox'/></td><td><input type ='checkbox'/></td><td><input type ='checkbox'/></td>";
        print "</tr>";
    }## Heading ##

7 个答案:

答案 0 :(得分:1)

这与我之前回答的问题有些相关:POST an array from an HTML form without javascript

  • 以表格
  • 包裹它们
  • 给他们一个“数组”名称
  • 他们在提交时的帖子中以数组结尾

相关项应该是这样的:name="item[collection name][collection name][]" - 注意与集合相关的第一个索引(为了便于定位),以及该集合中的空索引,有一个数组(而不是单个值)。所以对于你的复选框:

<input type="checkbox" name="answers[set1][]" value="apple" />   //imagine checked
<input type="checkbox" name="answers[set1][]" value="orange" />  //imagine checked
<input type="checkbox" name="answers[set1][]" value="grape" />
<input type="checkbox" name="answers[set2][]" value="airplane" />   //imagine checked
<input type="checkbox" name="answers[set2][]" value="train" />  //imagine checked
<input type="checkbox" name="answers[set2][]" value="boat" />
<input type="checkbox" name="answers[solo]" value="boar" /> //single type value. note that there is no [] in the end

在请求数组中像这样结束(比如说POST):

$_POST[] = array(
    'answers' => array(
        'set1' => array('apple','orange'),   //unchecked items won't be included
        'set2' => array('airplane','train'), //unchecked items won't be included
        'solo' => 'boar'
    )
);

<table>
    <?php foreach($stakeholderArray as $stakeholder): ?>
    <tr>

    <?php 

        //declare so these exist regardless of data
        $partner   = '';
        $agreement = '';
        $train     = '';
        $meet      = '';

        //if we have data, mark the boxes accordingly
        if($stakeholder['dataFound']){

            $checked = 'checked ="checked"';

            //mark as checked or blank
            $partner   = ($stakeholder['Partner'])   ? $checked: '';
            $agreement = ($stakeholder['Agreement']) ? $checked: '';
            $train     = ($stakeholder['Train'])     ? $checked: '';
            $meet      = ($stakeholder['Meet'])      ? $checked: '';

        }
    ?>

       <td><input value='partner' name="stake[<?= $stakeholder ?>][partner]" type ="checkbox" <?= $partner ?> /></td>
       <td><input value='agreement' name="stake[<?= $stakeholder ?>][agreement]" type ="checkbox" <?= $agreement ?> /></td>
       <td><input value='train' name="stake[<?= $stakeholder ?>][train]" type ="checkbox" <?= $train ?> /></td>
       <td><input value='meet' name="stake[<?= $stakeholder ?>][meet]" type ="checkbox" <?= $meet ?> /></td>

    </tr>
    <?php endforeach; ?>
</table>

他们最终会像:

$_POST[] = array(
    'stakeholder1' => array(
        'partner'=> 'partner',
        'agreement'=> 'agreement',
        'train'=> 'train',
        'meet'=> 'meet'
    ),
    'stakeholder2' => array(
        'partner'=> 'partner',
        'agreement'=> 'agreement',
        'train'=> 'train',
        'meet'=> 'meet'
    ),
);

答案 1 :(得分:0)

为每个name元素添加不同的checkbox标记(您需要添加name="WhatEverYouwant"

你可以通过以下方式获得它:

$_POST['ID Of the Element']

示例:

'<td><input type ="checkbox" name="new" checked ="checked" /></td>' : '<td><input type ="checkbox"  /></td>';

并通过以下方式获取:

$_POST['new']

答案 2 :(得分:0)

假设您已经拥有<form>,那么您需要为每个输入提供一个id。然后在生成的PHP脚本中,使用$_POST['whatever_the_name_is'](您也可以使用$_REQUEST$_GET,具体取决于您的表单操作。)

答案 3 :(得分:0)

更改:

<input type ="checkbox" ...

到:

<input type="checkbox" name="stakeholderarray[]" ...

或者:

<input type="checkbox" name="stakeholderarray[KEYNAME]" ...

在PHP中访问:

foreach($_POST['stakeholderarray'] as $this_stakeholderarray){
 ...
}

或者:

$_POST['stakeholderarray']['KEYNAME'];

这是有效的,因为添加到[]属性末尾的[KEYNAME] / name在PHP中被视为array项,因此可以循环播放。您也可以通过这种方式嵌套数组,因此如果您想在单个表单上拥有多个权益持有者,请执行以下操作:

<input type="checkbox" name="stakeholderarray[0][0]" ... <!-- Holder 0, item 0 -->
<input type="checkbox" name="stakeholderarray[0][1]" ... <!-- Holder 0, item 1 -->
<input type="checkbox" name="stakeholderarray[1][0]" ... <!-- Holder 1, item 0 -->
<input type="checkbox" name="stakeholderarray[1][1]" ... <!-- Holder 1, item 1 -->

答案 4 :(得分:0)

不是答案,但是,但是......减少逻辑中的一些重复的HTML:

print ($currentEntry['Partner'])     ? '<td><input type ="checkbox" checked ="checked" /></td>' : '<td><input type ="checkbox"  /></td>';

应该是

<td><input type ="checkbox"<?php ($currentEntry['Partner'] ? ' checked ="checked"' : '' ?> /></td>

答案 5 :(得分:0)

$i = 0;
foreach($stakholderArray as $currentEntry) {
  print "<tr class='$bgcolor'>";
  print "<td class='left'>$currentEntry[org]</td>";
  if($currentEntry['dataFound']) {  //if data was found for current stakeholder, display it
    print '<td><input type ="checkbox" name="chkPartner['.$i.']" '.(($currentEntry['Partner'])?'checked ="checked"':'').' /></td>';
  //print the rest like this
  $i++;
}

然后,您可以从$_POST访问它们

if(isset($_POST[chkPartner][$yourIndex]))
{}

答案 6 :(得分:-1)

Actualy id不应该有用。正如约瑟夫所说,表单元素以其名称作为键发送。所以正确的标签应该是:

<input type="checkbox" name="some_name" ... />

发送表单时,您可以获取$_POST['some_name']

等数据

如果您愿意,可以将它们放入数组name="somearr[someotherarr[some_name]]]",然后内容将在$_POST['somearr']['someotherar']['some_name']

中提供

希望有所帮助。