2

我有一个包含多行复选框的表单,每个复选框都有一个特定的 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 ##
4

7 回答 7

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'
    ),
);
于 2012-02-09T22:25:29.760 回答
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']
于 2012-02-09T22:20:59.097 回答
0

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

于 2012-02-09T22:22:07.297 回答
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'];

这是有效的,因为添加到属性末尾的[]/在 PHP中被视为一个项目,因此可以循环访问。你也可以用这种方式嵌套数组,所以如果你想在一个表单上拥有多个利益相关者,请执行以下操作:[KEYNAME]namearray

<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 -->
于 2012-02-09T22:29:38.260 回答
0

不是答案,而是 yeowch... 减少逻辑中的一些重复 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>
于 2012-02-09T22:32:25.800 回答
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]))
{}
于 2012-02-09T22:47:28.280 回答
-1

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

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

当您发送表单时,您可以获得如下数据$_POST['some_name']

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

希望有帮助。

于 2012-02-09T22:27:17.957 回答