从多个复选框行收集 $_POST

发布于 2025-01-04 07:48:53 字数 1558 浏览 1 评论 0原文

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

I have a form with multiple rows of checkboxes, each with a specific id, that are being displayed using a foreach loop.

How do you grab the $_POST info from something like that? I think it is like this somehow $_POST[][], like a sub-array, but I cant figure out how to set it up:

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 ##

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

久随 2025-01-11 07:48:54

这与我之前回答过的一个问题有些相关: 从没有 javascript 的 HTML 表单中发布数组

  • 将它们封装在表单中,
  • 给它们一个“数组”名称,
  • 时,它们最终会在发布过程中作为数组

当提交相关内容 项目应该是这样的: 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'
    ),
);

it's somewhat related to a question i answered before: POST an array from an HTML form without javascript

  • encase them in a form
  • give them an "array" name
  • they end up as an array during post when submitted

related items should have like this: name="item[collection name][collection name][]" - note the first indices pertaining the set (for easy location), and the empty index meaning in that set, there's an array (instead of single value). so for your check boxes:

<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

end up like this in the request array (like say 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>

they should end up like:

$_POST[] = array(
    'stakeholder1' => array(
        'partner'=> 'partner',
        'agreement'=> 'agreement',
        'train'=> 'train',
        'meet'=> 'meet'
    ),
    'stakeholder2' => array(
        'partner'=> 'partner',
        'agreement'=> 'agreement',
        'train'=> 'train',
        'meet'=> 'meet'
    ),
);
靑春怀旧 2025-01-11 07:48:54

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

,您将能够通过以下方式获取它: :

$_POST['ID Of the Element']

示例:

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

并通过以下方式获取:

$_POST['new']

Give a different name tag to every checkbox element (you need to add name="WhatEverYouwant")

and you will be able to get it by:

$_POST['ID Of the Element']

Example:

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

and get it by:

$_POST['new']
李白 2025-01-11 07:48:54

假设您已经在

中拥有了该内容,则需要为每个输入提供一个 id。然后在生成的 PHP 脚本中,使用 $_POST['whatever_the_name_is'] (您也可以使用 $_REQUEST$_GET,具体取决于您的表单行动)。

Assuming that you have that in a <form> already, you need to give each input an id. Then in the resulting PHP script, use $_POST['whatever_the_name_is'] (you could also use $_REQUEST or $_GET depending on your form action).

書生途 2025-01-11 07:48:54

不是答案,但是 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>

Not an answer, but yeowch... reduce some of the duplicate HTML in your logic:

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

should be

<td><input type ="checkbox"<?php ($currentEntry['Partner'] ? ' checked ="checked"' : '' ?> /></td>
朮生 2025-01-11 07:48:54

更改:

<input type ="checkbox" ...

收件人:

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

或:

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

在 PHP 中访问:

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

或:

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

这是有效的,因为添加到 name 属性末尾的 []/[KEYNAME] 在 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 -->

Change:

<input type ="checkbox" ...

To:

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

Or:

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

Access in PHP:

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

Or:

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

This works because []/[KEYNAME] added to the end of the name attribute is seen as an array item in PHP, and can therefore be looped through. You can nest arrays this way as well, so if you want to have multiple stake holders on a single form, do something like this:

<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 -->
勿忘初心 2025-01-11 07:48:54
$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]))
{}
$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++;
}

You can then access them from $_POST like so

if(isset($_POST[chkPartner][$yourIndex]))
{}
海之角 2025-01-11 07:48:54

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

当你发送表单时,你可以得到像 $_POST 这样的数据['some_name']

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

希望有帮助。

Actualy id shouldn't work. As Joseph said, form elements are sent with their names as keys. So the proper tag should be:

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

When you send the form, you can get the data like $_POST['some_name']

If you like you can put them in an array name="somearr[someotherarr[some_name]]]" then the content will be available in $_POST['somearr']['someotherar']['some_name']

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文