从多个循环Textarea中检索值

时间:2013-05-01 11:04:54

标签: php for-loop textarea

我已经建立了一个简短答案测验的表格。每个问题都有多个用于答案的文本框(对应于问题中的指令,即列出两个组件,三个优点等)。我已经设计了这样的表格,以便可以根据一组答案独立评估答案。每个问题可以有很多答案建议。这是答案'表结构:

CREATE TABLE IF NOT EXISTS `answer` (
`a_id` int(10) NOT NULL AUTO_INCREMENT,
`q_id` int(10) NOT NULL,
`a_text` text NOT NULL,
`a_keyword` text NOT NULL,
PRIMARY KEY (`a_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;

这是表格的代码:

<?php
session_start();
if(isset($_SESSION['tf1_sid'])){
?>

<head>
<title>Dahlia | Formative Assessment</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<form id="form1" name="form1" method="post" action="s_ass_result_new.php">
<h2>Short-answer Questions</h2>
<table width="590" border="0" cellpadding="2" align="center">
<?php
// db connect
include("dbconn.php");

// db query for questions
$sql_q  = "SELECT q_id, q_no, q_text, q_field FROM question";
$query_q = mysql_query($sql_q) or die("MySQL Error: " . mysql_error());
// start loop for questions
//$rad = 1;
while($data_q = mysql_fetch_array($query_q, MYSQL_ASSOC)){     

$qfield = $data_q['q_field'];
$qno = $data_q['q_no'];

echo "<tr><td width='20' align='center' valign='top'><label><br /><input name='q_no' size='1' type='hidden' value=". $data_q['q_no'] .">". $data_q['q_no'] ."</label></td>";
echo "<td><br />". $data_q['q_text'] ." (<a href='s_help.php?s_id=". $_SESSION['tf1_sid'] ."&q_id=". $data_q['q_id'] ."' target='_blank'>Help</a>)</td>";

for($rad=1;$rad<=$qfield;$rad++){
echo "<tr><td></td><td><textarea name='answer_".$rad."' cols='55' rows='2' id='textarea1'></textarea></td></tr>";
echo "<script>document.getElementById('textarea1').focus()</script>";
//$rad++;
}
}
echo "<tr></tr><tr><td></td><td><input name='Submit' type='submit' value='Submit' onClick='return confirm(\"Are you sure?\")'></td></tr>";      
mysql_free_result($query_q);
include("dbconn.php");
?>
</table>
</form>
</body>
</html>
<?php
}
else
{
header("Location:s_login.php");
}
?>

表单显示正常,但我无法检索通过文本区域提交的值。下面是检索这些值的代码:

<?php
// include db connection file
include("dbconn.php");
session_start();

if(isset($_POST['Submit']))
{   
$id = $_SESSION['tf1_sid'];
$qno = $_POST['q_no'];
$ansspc = $_POST['q_field'];
?>

<head>
<title>Dahlia | Formative Assessment</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h2>Answer Review</h2>
<table width="590" border="0" cellpadding="2" align="center">
<?php

//db query to obtain i_id - to insert to RESULT table
$sql_i = "SELECT i_id FROM ins_stud WHERE s_id = '$id'";
$query_i = mysql_query($sql_i) or die("MySQL Error: " . mysql_error());
$data_i = mysql_fetch_assoc($query_i);
$ins_id = $data_i['i_id'];

//$j = 1;
$arr_ind = 1;
$atext = array(1);
$ans = array(1);

//FOR LOOP TO RETRIEVE VALUES FROM TEXT AREA
for($i=1;$i<=$q_no;$i++){
for($k=1;$k<=$ansspc;$k++){
$repStr = str_replace("1", $k, "answer_1");
echo "Question ". $i .": Answer: ". $repStr;
$ans[] = $_POST[$repStr];
echo $ans;
}

$sql_check = "SELECT a_text FROM answer WHERE q_id='$i'";
$query_ch = mysql_query($sql_check) or die("MySQL Error: " . mysql_error());
$data_ch = mysql_fetch_assoc($query_ch);
$atext[] = $data_ch['q_ans'];       
//$j++;
}
...

之前我的问题是每个问题设置一个文本区域。我打算做的是将每个答案与每个答案建议进行比较。

示例:

陈述......的两个优点。

  1. 两个文字区:ans1,ans2

  2. 四个建议答案:soln1,soln2,soln3,soln4

  3. 所以我将ans1与soln1,soln2,soln3,soln4进行比较,然后对ans2进行比较。

    编辑:

    //start loop for questions & answers
    while($data_q = mysql_fetch_array($query_q, MYSQL_ASSOC)){     
    
    //process returned data
    foreach ($_POST['answer'] as $questionNumber => $answerList){
    echo "question " . $questionNumber . ": \n";
    foreach ($answerList as $key => $answer){
    $ans[] = $answer;
    echo "answer " . $key . ": " . $answer . "\n";
    }//end foreach_in
    }//end foreach_out
    
    $jawapan = explode(" ", $ans[$arr_ind]);
    $jwpn = trim($jwpn);
    
    foreach($jwpn as $eval){
    if (stripos($atext[$arr_ind], $eval) !== false){ //$atext: query for answer suggestions
    //answer match with first suggestion
    echo "<p align='justify'><img src='image/mark.png' border='0' width='20' height='20'> ". $ans[$arr_ind]. "</p>";
    }//if
    {else
    //answer doesn't match with first suggestion; check next suggestion
    for($m=1;$m<=$box;$m++){...}// $box = no. of text area in form
    }//else
    
    echo "<p align='justify'><label><b>SUGGESTED ANSWER:</b> <br><input name='answer_".$rad."' type='hidden' value=''>". $atext[$arr_ind] . "</label></p>";
    
    $arr_ind++;
    }//foreach
    

1 个答案:

答案 0 :(得分:1)

最好是在textareas的命名中使用数组

输出textarea(为了清楚起见,我省略了所有标记附加内容)     while($ data_q = mysql_fetch_array($ query_q,MYSQL_ASSOC)){

    $qfield = $data_q['q_field'];
    $qno = $data_q['q_no'];

    for ($rad = 1; $rad <= $qfield; $rad++) {
        echo "<textarea name='answer[" . $qno . "][" . $rad . "]' id='textarea_" . $qno . "_" . $rad . "'>";
    }
}

并处理返回的数据

foreach ($_POST['answer'] as $questionNumber => $answerList) {
    echo "question " . $questionNumber . ": \n";
    foreach ($answerList as $key => $answer) {
        echo "answer " . $key . ": " . $answer . "\n";
    }
}