我现在有一个表单,它通过一个for循环,相应地循环显示我希望它出现的选项的次数。
以下是代码:
<?php for ($i =1; $i <=20; $i++) { ?>
<tr>
<td><center><input type="text" name="qnsNo[]" class="qnsNo"/></center></td>
<td><input type="text" name="question[]" class="question"/></td>
<td><select name="input[]" class="dropdown">
<option value="Normal text line, no input required*">Normal text line, no input required*</option>
<option value="Multiple choice (only one answer) [Radio Button]">Multiple choice (only one answer) [Radio Button]</option>
<option value="Multiple choice (multiple answers) [Check box]">Multiple choice (multiple answers) [Check box]</option>
<option value="Drop-Down Box">Drop-Down Box</option>
<option value="Comment/Essay Box [Textarea]">Comment/Essay Box [Textarea]</option>
<option value="Single Line Textbox">Single Line Textbox</option>
</select></td>
<td><input type="text" name="options[]" class="option"/></td>
<td><input type="text" name="others[]" class="others"/></td>
<input type="hidden" name="compulsory[]" value=""/>
<td><center><input type="checkbox" name="compulsory[]" value="Compulsory"/></center></td>
</tr>
<?php } ?>
这个表格实际上做的是允许人们创建自己的表单,最后一栏是“必修字段?”允许他们选择是否强制要求这个问题。但是目前我似乎无法将复选框结果正确地插入到数据库中。
我听说如果你想在数据库中插入一个未经检查的复选框答案,你可以在实际的复选框行之前放置隐藏字段。我试过我的运气,插入部分仍然不正确.. 是否有任何方法可以根据未选中并检查来插入值? 就像它未经检查一样,将为'null',如果已经选中,则会插入'强制'...
如果需要,这是插入数据库的代码:
$sql_array = array();
foreach ($_POST['question'] as $row => $name) {
$question = $name;
$qnsNo = $_POST['qnsNo'][$row];
$input = $_POST['input'][$row];
$options = $_POST['options'][$row];
$others = $_POST['others'][$row];
$compulsory = $_POST['compulsory'][$row];
$idQuery = "SELECT max(surveyID) FROM scSurveyForm WHERE createBy = '$createBy' AND writeUp = '$writeUp'";
$idResult = sqlsrv_query($conn, $idQuery);
$rows = sqlsrv_fetch_array($idResult);
$lastID = $rows[0];
$sql_array[] = "('" . $question . "'," . $lastID . ",'" . $qnsNo . "','" . $input . "','" . $options . "','" . $others . "','" . $compulsory . "')";
if (!empty($question)) {
$query_single = "INSERT INTO scFormLayout(question, surveyID, qnsNo, input, options, others, compulsory)
VALUES" . implode(', ', $sql_array);
echo $query_single.'<br/>';
$status = sqlsrv_query($conn, $query_single);
$sql_array = array();
}
}
答案 0 :(得分:0)
在您的复选框名称中添加号码。
<input type="checkbox" name="compulsory[<?php echo $i; ?>]" ... >
并测试$_POST
// you get 'on' or NULL
$compulsory = isset($_POST['compulsory'][$row]) ? $_POST['compulsory'][$row] : NULL;
或更短:
// you get TRUE or FALSE
$compulsory = isset($_POST['compulsory'][$row]);