无法理解为什么设置数组但处理后数组为空

时间:2013-03-29 04:14:35

标签: php arrays associative

第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
?>

2 个答案:

答案 0 :(得分:3)

您的复选框名称应如下所示,将其视为数组

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

答案 1 :(得分: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; ?>