-2

Line 42 is the error. I'm not sure as to why it keeps saying it's not an array is one section but fails to find the array on line 42. I have tried to change the line to ($_POST['CINS'] as $cNum => $v) and ($CINS as $cNum => $v). Any insight or help would be appreciated.

<?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
?>
000
  • 26,951
  • 10
  • 71
  • 101
Nick
  • 1
  • 1

2 Answers2

3

Your checkbox name should like below to consider it as an array

<input type="checkbox" name="CINS[]" value = "1" />
Nauphal
  • 6,194
  • 4
  • 27
  • 43
  • The naming convention has nothing to do with `$_POST` being handled as an array. – Phillip Berger Mar 29 '13 at 04:22
  • @aguyfromhere: its ok for `$_POST`. what about `$_POST['CINS']` ? – Nauphal Mar 29 '13 at 04:24
  • @nauphal, when you pass a named item in HTML to a PHP handler, the superglobals $_POST and $_GET and treated as objects and therefore have nested arrays inside them already. An HTML input with name CINS is automatically referred to as $_REQUEST['CINS']. – Phillip Berger Mar 29 '13 at 04:29
  • @aguyfromhere I am confused, are you proposing that the name of the checkbox is correct to be `CINS` and not `CINS[]`? – 000 Mar 29 '13 at 04:33
  • @aguyfromhere: dont understand what you are trying to say. – Nauphal Mar 29 '13 at 04:33
  • @aguyfromhere: just check OP of this `is_array($_POST['CINS'])` while the name of the checkbox is just `CINS` and `CINS[]` – Nauphal Mar 29 '13 at 04:35
0

The <input> name is an array, and I've found a spare </p> that shouldn't be there.

Your coding style is going to cause A LOT of HEADACHES. I've cleaned it up a bit, I think this style will be much easier to handle.

<?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; ?>
000
  • 26,951
  • 10
  • 71
  • 101
  • To be honest, I'm not really sure where that tag came from. Thought I had taken that out. but thanks for pointing it out. – Nick Mar 29 '13 at 16:27
  • Have you found resolution for this problem? – 000 Mar 29 '13 at 19:03