2013-03-29 63 views
-2

第42行是錯誤。我不知道爲什麼它一直說它不是一個數組是一個部分,但未能在第42行找到數組。我嘗試將行更改爲($ _POST ['CINS']爲$ cNum => $ v)和($ CINS爲$ cNum => $ v)。任何見解或幫助將不勝感激。無法理解數組爲何設置但陣列爲空後爲空

<?php 
$title = "fTest.php"; 
$action=$_SERVER['PHP_SELF']; 
include("html-head.inc"); 
echo <<<HEREDOC 
<header> 
<h1>$title</h1> 
</header> 
HEREDOC; 

if (!isset($_POST['submit'])) 
{ 
     echo "<form method=\"post\" action=\"$action\">"; 
$CINS = array('101' => "CINS101", 
       '108' => "CINS108", 
       '121' => "CINS121", 
       '251' => "CINS251", 
       '254' => "CINS254"); 
     echo "<p>Please pick your CINS classes:</p>"; 
     echo "<ul>\n"; 
foreach ($CINS as $key => $value) 
{ 
     echo "<li>"; 
     echo "<input type=\"checkbox\" name=\"CINSc\" value=\"$value\"/>CINS$key" ; 
     echo "</li>\n"; 
}     
     echo "</ul>\n"; 
     echo "<input type=\"reset\" name=\"reset\" value\"Reset\" />"; 
     echo "<input type=\"submit\" name=\"submit\" value\"Submit\" />"; 
     echo "</p>"; 
echo is_array($CINS) ? 'Array' : 'Not an array'; 
echo "\n"; 
     echo "</form>"; 
} // ends IF PORTION for ISSET 
else  
{   
if (count($_POST['CINS'] > 0)) 
{   
     echo "<h2> Your picks are: </h2>\n"; 
     echo "<ul>\n"; 
echo is_array($CINS) ? 'Array' : 'Not an array'; 
foreach ($_POST['CINS'] as $cNum => $v) //This is the error. 
{ 
     echo "\t<li>$v</li>\n"; 
} // end of FOREACH cins 
     echo "</ul>\n"; 
} // end of IF count CINS 
} // end of ELSE portion for ISSET 
?> 
+1

請問你可以在這裏粘貼數組print_r($ _ POST ['CINS']); –

+0

唯一出現的是無效的參數錯誤 – Nick

回答

3

你的複選框對象的名稱想下面把它看作一個數組

<input type="checkbox" name="CINS[]" value = "1" /> 
+0

命名約定與'$ _POST'作爲數組處理無關。 –

+0

@aguyfromhere:它適用於'$ _POST'。那麼'$ _POST ['CINS']'呢? – Nauphal

+0

@aguyfromhere http://www.php.net/manual/en/faq.html.php#faq.html.arrays –

0

<input>名字是一個數組,我發現一個備用</p>是不應該存在。

您的編碼風格將導致大量的頭痛。我已經清理了一下,我認爲這種風格會更容易處理。

<?php 
$title = "fTest.php"; 
$action=$_SERVER['PHP_SELF']; 
include("html-head.inc"); 
?> 

<header> 
<h1><?=$title?></h1> 
</header> 

<?php if (!isset($_POST['submit'])): ?> 
    <?php $CINS = array('101' => "CINS101", 
         '108' => "CINS108", 
         '121' => "CINS121", 
         '251' => "CINS251", 
         '254' => "CINS254"); ?> 
    <form method="post" action="<?=$action?>"> 
     <p>Please pick your CINS classes:</p> 
     <ul> 
     <?php foreach ($CINS as $key => $value): ?> 
      <li> 
      <input type="checkbox" name="CINS[]" value="<?=$value?>" />CINS<?=$key?> 
      </li> 
     <?php endforeach; ?> 
     </ul> 
     <input type="reset" name="reset" value"Reset" /> 
     <input type="submit" name="submit" value"Submit" /> 
     </p> <!-- THIS TAG IS EXTRA WHERE DID IT COME FROM --> 
     <?= is_array($CINS) ? 'Array' : 'Not an array' ?> 
    </form> 
<?php else if (count($_POST['CINS'] > 0)): ?> 
    <h2>Your picks are: </h2> 
    <ul> 
    <?= is_array($CINS) ? 'Array' : 'Not an array' ?> 
    <?php foreach ($_POST['CINS'] as $cNum => $v): ?> 
     <li><?=$v?></li> 
    <?php endforeach; ?> 
    </ul> 
<?php endif; ?> 
+0

老實說,我不太確定在哪裏該標籤來自於。我以爲我已經把它解決了。但感謝您指出。 – Nick

+0

您是否找到解決此問題的解決方案? –