PHP:使用表单值填充数组

时间:2015-09-17 16:41:12

标签: php arrays forms for-loop populate

我有一份包含5个问题的问卷表,每个问题有5个单选按钮选项。现在,我想按下提交按钮捕获此表单数据。我在使用POST方法发送的这些单选按钮的值填充数组时遇到困难。

我写的代码是抛出错误:“注意:第11行的E:\ xampp \ htdocs \ dataCollect \ form1.php中的未定义索引:'radio11'

代码如下:

<?php
if (isset($_POST['btnForm1'])){
for($count = 1; $count < 6; $count++){
$radioName = "radio1".$count;
$arrForm1 = array($_POST["'".$radioName."'"]);
}}
<?>
<html>
<body>
<form name="ESForm" action="form1.php" method="post">
<table name="ESTable" style="oTab1">
<tr>
    <td style="oRow">1. AAAAAA <br>
    <input type="radio" name="radio11" value="1">Never<br>
    <input type="radio" name="radio11" value="2">Seldom<br>
    <input type="radio" name="radio11" value="3">Sometimes<br>
    <input type="radio" name="radio11" value="4">Often<br>
    <input type="radio" name="radio11" value="5">Always<br>
    </td>
</tr>
<tr>
    <td style="oRow">2. AAAAAA<br>
    <input type="radio" name="radio12" value="1">Never<br>
    <input type="radio" name="radio12" value="2">Seldom<br>
    <input type="radio" name="radio12" value="3">Sometimes<br>
    <input type="radio" name="radio12" value="4">Often<br>
    <input type="radio" name="radio12" value="5">Always<br>
    </td>
</tr>
<tr>
    <td style="oRow">3. AAAAA <br>
    <input type="radio" name="radio13" value="1">Never<br>
    <input type="radio" name="radio13" value="2">Seldom<br>
    <input type="radio" name="radio13" value="3">Sometimes<br>
    <input type="radio" name="radio13" value="4">Often<br>
    <input type="radio" name="radio13" value="5">Always<br>
    </td>
</tr>
<tr>
    <td style="oRow">4. AAAaA <br>
    <input type="radio" name="radio14" value="1">Never<br>
    <input type="radio" name="radio14" value="2">Seldom<br>
    <input type="radio" name="radio14" value="3">Sometimes<br>
    <input type="radio" name="radio14" value="4">Often<br>
    <input type="radio" name="radio14" value="5">Always<br>
    </td>
</tr>
<tr>
    <td style="oRow">5. AAAAA <br>
    <input type="radio" name="radio15" value="1">Never<br>
    <input type="radio" name="radio15" value="2">Seldom<br>
    <input type="radio" name="radio15" value="3">Sometimes<br>
    <input type="radio" name="radio15" value="4">Often<br>
    <input type="radio" name="radio15" value="5">Always<br>
    </td>
</tr>
<tr>
    <td><input type="submit" name="btnForm1" value="Submit"></td>       
</tr>
</table>
</form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

这是不正确的:

$arrForm1 = array($_POST["'".$radioName."'"]);
                         ^^^^----------^^^^----useless

"'"引号绝对是不必要的。这就是你应该拥有的一切:

$arrForm1 = array($_POST[$radioName]);

为什么将单选按钮的值包含在数组中。你只会从每个单选按钮集中取回一个值。