I got the following error when I try to display the post array element. How can I fix it?
Notice: Undefined index: ans in C:
Warning: Invalid argument supplied for foreach() in C:
HTML CODE
<input type = "radio" name = "ans[<?php echo $id; ?>]"
value="A"/>A. <?php echo $A; ?><br />
<input type = "radio" name = "ans[<?php echo $id; ?>]"
value="B"/>B. <?php echo $B; ?><br />
<input type = "radio" name = "ans[<?php echo $id; ?>]"
value="C"/>C. <?php echo $C; ?><br />
<input type = "radio" name = "ans[<?php echo $id; ?>]"
value="D"/>D. <?php echo $D; ?><br /><br />
PHP CODE
<?php
$db= new mysqli("localhost","root","","test");
if ($db->connect_error) {
echo "error connect database".$db-connect_error;
} else{
mysql_select_db("test") or die ("Unable to select database: " .mysql_error());
}
foreach($_POST['ans'] as $option_num => $option_val) {
echo $option_num." ".$option_val."<br>";
}
?>