表单中的数组值

时间:2019-04-18 09:35:48

标签: php arrays forms radio-group

我有一个表格,其中最多包含10组单选按钮。单选按钮提供问题的答案。从数据库中填充所有问题,完成后,将使用字段user_id,question_number和answer_value更新数据库。我正在使用一个数组,但是似乎只有一个answer_value传递给表单处理器。

这是我的代码

    <form action="new-answers.php" method="post" id="my_answer">    
    <div class="form-group">
    <label class="radio-inline"><input name="answer_value[]" type="radio" required="required" value="1">&nbsp;Totally false for me&nbsp;</label>
    <label class="radio-inline"><input name="answer_value[]" type="radio" required="required" value="2">&nbsp;Partially false for me&nbsp;</label>
    <label class="radio-inline"><input name="answer_value[]" type="radio" required="required" value="3">&nbsp;Indifferent/neutral&nbsp;</label>
    <label class="radio-inline"><input name="answer_value[]" type="radio" required="required" value="4">&nbsp;Partially true for me&nbsp;</label>
    <label class="radio-inline"><input name="answer_value[]" type="radio" required="required" value="5">&nbsp;Totally true for me&nbsp;</label>

这是我在表单处理器页面中使用的

if (isset($_POST['submit'])) {
$i=0;

$user=$_POST['user_id'];
$question=$_POST['question_number'];
$answer = $_POST['answer_value']; 
// open the mysqli prepared statement globally
$stmt = $mysqli -> prepare('INSERT INTO answers (user_id,question_number,answer_value) VALUES (?, ?,?)');

function insertAnswer($user, $question, $answer) {
    global $stmt;

    // using prepared statement several times with different variables
    if (
        $stmt &&
        $stmt -> bind_param('iii', $user, $question,$answer) &&
        $stmt -> execute()
    ) {
        // new answers added
    }

}
}

即使我尝试使用

进行回显
$answer = $_POST['answer_value'];

foreach($answer as $value) {
    echo $value.'<br />';
}

我只能得到答案。

0 个答案:

没有答案