2012-02-09 19 views
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 ## 
+0

輸入名稱可以是一個變量數組嗎?像我仍然無法讓它工作..這是一個截圖,如果它有幫助https://plus.google.com/u/0/photos/102528100285286815171/相冊/ 5708634782606194689/5708634785261526130 – user718229 2012-02-13 15:03:11

回答

1

這有點相關的問題,我纔回答:POST an array from an HTML form without javascript

  • 包裹於一個形式
  • 給他們一個「陣列」的名字
  • 提交
  • ,當他們最終成爲後在一個數組

相關的項目應該有這樣的:name="item[collection name][collection name][]" - 注意有關該集合(方便的位置)的第一指標,而空指數在臨意義t集合,有一個數組(而不是單個值)。所以對於您的複選框:

<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' 
    ), 
); 
+0

https://plus.google.com/ u/0/photos/102528100285286815171/albums/5708634782606194689/5708634785261526130 – user718229 2012-02-13 15:01:41

+0

檢查此更新的答案 – Joseph 2012-02-13 20:33:11

+0

非常感謝。它就像你所描述的那樣工作。現在我只需要弄清楚如何分割它。謝謝 – user718229 2012-02-14 17:08:19

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'] 
+0

它不是由'$ _POST'拾取的'id',而是'name'屬性 – 2012-02-09 22:28:11

+0

ooppss ...編輯它,謝謝:) – 2012-02-09 22:29:43

0

假設你有一個<form>已經,你需要給每個輸入自己的ID。然後在生成的PHP腳本中,使用$_POST['whatever_the_name_is'](您也可以使用$_REQUEST$_GET,具體取決於您的表單操作)。

-1

Actualy id should not work。正如約瑟夫所說,表單元素是以他們的名字作爲關鍵字發送的。所以正確的標籤應該是:

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

當您發送的形式,你可以像$_POST['some_name']

的數據。如果你喜歡,你可以把它們放在一個數組name="somearr[someotherarr[some_name]]]"則內容將可在$_POST['somearr']['someotherar']['some_name']

希望有所幫助。

+0

'somearr [someotherarr [some_name]]]'不能達到你認爲的效果。 – Josh 2012-02-09 22:31:26

+0

是的,我糾正了。單層數組雖然可以'array [key]'工作。 – 2012-02-09 22:42:45

+0

多層次的工作。使用'somearr ['someotherarr'] ['some_name']'來代替,這將達到你最初的預期。 :) – Josh 2012-02-09 22:46:34

0

變化:

<input type ="checkbox" ... 

要:

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

或者:在PHP

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

訪問:

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 --> 
+0

這裏的問題是你不會知道'$ _POST [stakeholderarray] []'數組中的哪些元素屬於哪一行復選框。只有被選中的那些被存儲在數組中。 – 2012-02-09 22:32:09

+0

稍微改變了答案 - 沒有想到他讓每個人都分配了一個特定的值,而不是一個項目列表。 – 0b10011 2012-02-09 22:34:12

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> 
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++; 
} 

然後,您可以從訪問它們like so

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