美好的一天!我是PHP的新手,想寻求帮助。
我有一个用于从数据库自动生成问题的代码以及5个复选框。我的问题是,如何从复选框中保存其他值?
任何想法将不胜感激!
<?php
$sql = "SELECT * FROM question_pool_registrar WHERE criteria_id = 1";
$result = $connect->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
$question = $row['question'];
echo "<tr>
<td>$question</td>
<td><input type='checkbox' value='1C-Q1-5' name='answer1' class='radio'></td>
<td><input type='checkbox' value='1C-Q1-4' name='answer1' class='radio'></td>
<td><input type='checkbox' value='1C-Q1-3' name='answer1' class='radio'></td>
<td><input type='checkbox' value='1C-Q1-2' name='answer1' class='radio'></td>
<td><input type='checkbox' value='1C-Q1-1' name='answer1' class='radio'></td>
</tr>";
}
}
?>
答案 0 :(得分:0)
复选框的行为是,它们的输入标签要求具有相同的名称,但它们将被视为数组,因此您需要添加** [] **他们的名字大括号
<input type='checkbox' value='1C-Q1-5' name='answer1[]' class='radio'>
现在,取决于您将使用GET还是POST方法:
$_POST['answer1'] //this is an array
$answers = $_POST['answer1']
// we pass the fetched data first
// then we transform then into a string if you want to save them in your database
$final_answers = implode("," , $answers);
//the $final_answers variable is a STRING that now holds all the chosen checkboxes but are separated by a comma
//for example, it will hold a value "1C-Q1-5, 1C-Q1-2, 1C-Q1-1", if the user has checked the 1st, 2nd and 5th checkboxes