我下面的代码应该显示每个问题($_POST[questionText])。对于每个问题,它显示它的 SessionId、QuestionId、QuestionContent 和 OptionId。但相反,它只显示 1 个问题。为什么它只显示 1 个问题,我怎样才能让它显示所有问题?
我使用回显来使用 INSERT VALUES 对输出进行文本处理。
foreach($_POST['questionText'] as $i => $question)
{
$insertquestion = array();
$options[] = $_POST['gridValues'];
switch ($options[$i]){
case "3":
$selected_option = "A-C";
break;
case "4":
$selected_option = "A-D";
break;
default:
$selected_option = "";
break;
}
$optionquery = "SELECT OptionId FROM Option_Table WHERE (OptionType = '". mysql_real_escape_string($selected_option)."')";
$optionrs = mysql_query($optionquery);
$optionrecord = mysql_fetch_array($optionrs);
$optionid = $optionrecord['OptionId'];
$insertquestion[] = "'". mysql_real_escape_string($_SESSION['id'] ) . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '') ."' ,'". mysql_real_escape_string( $_POST['num_questions'] ) ."','". mysql_real_escape_string( $question ) ."','". mysql_real_escape_string( $optionid ) ."'";
$questionsql = "INSERT INTO Question (SessionId, QuestionId, QuestionContent, OptionId)
VALUES (" . implode('), (', $insertquestion) . ")";
$i++;
}
echo($questionsql);
下面是javascript代码和表单代码。它的工作原理是用户在文本区域中输入一个问题('name='questionText')
并输入一个选项(name='gridValues')
,然后他们将它们两个附加到一个表格行中(表格形式为 which id='qandatbl'
)。这是问题1。然后他们对第二个问题再次做同样的事情,然后是第三个等等。请仔细看这个,很容易理解。
<script>
function insertQuestion(form) {
var context = $('#optionAndAnswer');
var $tbody = $('#qandatbl > tbody');
var $tr = $("<tr class='optionAndAnswer' align='center'></tr>");
var $question = $("<td class='question'></td>");
var $options = $("<td class='option'></td>");
var $questionType = '';
$('#questionTextArea').each( function() {
var $this = $(this);
var $questionText = $("<textarea class='textAreaQuestion'></textarea>").attr('name',$this.attr('name')+"[]")
.attr('value',$this.val())
$question.append($questionText);
});
$('.gridTxt', context).each( function() {
var $this = $(this);
var $optionsText = $("<input type='text' class='gridTxtRow maxRow' />").attr('name',$this.attr('name'))
.attr('value',$this.val())
$options.append($optionsText);
$questionType = $this.val();
});
$tr.append($question);
$tr.append($options);
$tbody.append($tr);
}
</script>
<form id="QandA" action="insertQuestion.php" method="post" >
<h1>SESSION (<?php echo $_SESSION['id'] ?>)</h1>
<table>
<tr>
<td rowspan="3">Question:</td>
<td rowspan="3">
<textarea id="questionTextArea" rows="5" cols="40" name="questionText"></textarea>
</td>
</tr>
</table>
<table id="optionAndAnswer" class="optionAndAnswer">
<tr class="option">
<td>Option Type:</td>
<td>
<div>
<input type="text" name="gridValues" class="gridTxt maxRow" readonly="readonly" />
</div>
</td>
</tr>
</table>
<table id="qandatbl" align="center">
<thead>
<tr>
<th class="question">Question</th>
<th class="option">Option Type</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</form>