你能帮我修复我的代码

时间:2018-06-17 00:24:09

标签: php

当我尝试显示post数组元素时出现以下错误。我该如何解决?

  

注意:未定义的索引:C:中的ans

     

警告:在C:

中为foreach()提供的参数无效

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代码

<?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>";
    }
?>

1 个答案:

答案 0 :(得分:0)

您无法使用var search_notes = $(this).val().toLowerCase(); $('.task, .notes').css('display', 'none'); $('div.notes').filter(function() { return $(this).text().toLowerCase() === search_notes; }).forEach(function() { $(this) .css('display', 'block') .prev() .css('display', 'block'); }); var count = $('div.notes').filter(function() { return $(this).css('display') !== 'none'; }).length; $('#count').text(count); ,因为您的name属性的值不仅仅是$_POST['ans']。相反,它们打印为'ans'。例如,假设您的答案选项A中的ID为1,那么我们将'ans' + the id you echoed 'ans[1]'

因此,一般情况下,您必须处理使您的名称属性完整的字符串部分,该部分来自$_POST['ans[1]'],这意味着如果$ id为1,则<?php echo $id; ?>name = "ans[<?php echo $id; ?>]"相同,您将以name = "ans[1]"的身份申请。

希望这会帮助你。不客气。