这是我动态生成的表单,允许用户输入学生的分数。我的挑战是如何捕获特定的 subjectCode 及其分数,因为我使用 subjectcode 存储在数据库中。
<?php
$attributes=array('class'=>'','id'=>'');
echo form_open('scoreInsertion/insertScores',$attributes);
echo "<table>";
echo "<tr>";
echo "<td>";?>
<label for="admNo>">Admission Number<span class="required">*</span></label>
<?php echo form_error('admNo');
echo "</td>";
echo "<td>";?>
<input type="text" name="admNo" id="admNo" value="<?php echo set_value('admNo')?>" />
<?php echo "</tr>";
echo "<tr>";
echo "<td>";
?>
<label for="studentName>">Student Name<span class="required">*</span></label>
<?php echo form_error('studentName');
echo "</td>";
echo "<td>";?>
<input type="text" name="studentName" id="studentName" value="<?php echo $studentName?>" />
<?php echo "</tr>";
echo "<tr></tr>";
echo "<tr>";
echo "<td>";
?>
<label for="examCategory">Exam Category<span class="required">*</span></label>
<!--<tr><td><?php echo form_error('examCategory')?></td></tr>-->
<td>
<select name="examCategory">
<option value="">--Select Category--</option>
<?php
print_r($exams);
if(isset($exams) && is_array($exams)){
foreach($exams as $exam){
$examId=$exam->id;
$examName=$exam->category;
echo "<option value='$examId'>$examName</option>";
}
}
?>
</select></td><?php
echo "<tr></tr>";
echo "<tr>";
echo "<td>";
?>
<label for="term">Academic Term<span class="required">*</span></label>
<!--<tr><td><?php echo form_error('examCategory')?></td></tr>-->
<td>
<select name="term">
<option value="">--Academic Term--</option>
<?php
if(isset($terms) && is_array($terms)){
foreach($terms as $term){
$termId=$term->termId;
$termName=$term->name;
echo "<option value='$termId'>$termName</option>";
}
}
?>
</select></td>
<?php
echo "<tr>";
//loop through the subjects array and display them on the view
if(isset($subjects) && is_array($subjects)){
foreach($subjects as $subject){
$subjectName=$subject->subName;
$subjectCode=$subject->subCode;
echo "<tr>";
echo "<tr></tr>";
echo "<td>";?>
<label for="<?php echo $subjectName?>"><?php echo $subjectName;?><span class="required">*</span></label>
<?php echo form_error($subjectName);
echo "</td>";
echo "<td>";?>
<input type="text" name="<?php echo $subjectCode?>" id="<?php echo $subjectName?>" value="<?php echo set_value($subjectCode)?>" />
<?php echo "</tr>";
}
}
?>
<?php echo "<tr>";echo "<td>";
echo form_submit('submit','Submit');
echo "</td>";
echo "</tr>";
?>
</p>
<?php
echo "</table>";
echo form_close();
?>
用于处理上述 POST 详细信息的 PHP 脚本如下:
function insertScores(){
$admNo=$this->input->post('admNo');
$term=$this->input->post('term');
//get the post variables from the Form
$posted = $this->input->post();
$x = array_keys($posted);
foreach($x as $y) {
echo $y ." = ". $_POST[$y]."<br/>";
echo "<br>";
//write the sql
$form_data=array(
'admNo' =>$admNo,
'subCode'=>$y,
'termId'=>$term,
'score'=>$_POST[$y]
);
print_r($form_data);
$this->SaveForm($form_data);
}
}