表中的单选按钮 - 用php脚本命名每一行(Likert Scale)

时间:2014-04-15 15:39:19

标签: javascript php html html-table radio-button

我已经构建了一个简单的 Likert scale 应用程序,它允许用户根据需要添加尽可能多的问题(代码如下,您可以尝试)。

因此,用户在第一页( index.html )上创建问题(like),然后通过提交,他们有一个类似调查的页面( execute.php )提出他们的缩放比例问题。

问题:问题是一切正常,除了命名输入单选按钮以便以后通过某些PHP脚本捕获它。它被命名,但不是每一行都有相同的名称,因此Likert标度不起作用。它的工作方式如下:每个问题的每一行单选按钮都应该有一个名称。

FOR EXAMPLE :对于第一个问题:第一行中的单选按钮应该具有名称​​ likert11,likert11,likert11 ......第二行应该是 likert12,likert12,likert12 ...... 对于问题2:它应该是 likert21,likert21 ......

INDEX.HTML

<input type="submit" value="LIKERT" onclick="addlik();" />
<form action="execute.php" method="post">
<div id="brain1"></div><br/>
<input type="submit" name="submit" value="OK"><br/>
</form>

的script.js

n=1;
function addlik() {
        var textarea = document.createElement("textarea");
        textarea.name = "question[" + n + "]";
        textarea.rows = 2;
        textarea.cols = 60;
        var lik = document.createElement("textarea");
        lik.name = "lik[" + n + "]";
        lik.rows = 6;
        lik.cols = 30;
        var liko = document.createElement("textarea");
        liko.name = "liko[" + n + "]";
        liko.rows = 6;
        liko.cols = 30;
        var div = document.createElement("div");
        div.innerHTML = n + ". Question: <br />" + textarea.outerHTML + "<br />The left textbox is for Items (vertical). The rigth one is for Options (header horizontal): <br/>" + lik.outerHTML + liko.outerHTML + "<br /><hr/><br/>";
        document.getElementById("brain1").appendChild(div);
        n++;
}

EXECUTE.PHP

<?php
$bla = "";
$question = $_POST['question'];
$length = count($_POST['question']);
$likoprint = array("","","","","","","","","","","","","","","","","","","","","");
$tablerow = array("","","","","","","","","","","","","","","","","","","","","");
$radiooptions = array("","","","","","","","","","","","","","","","","","","","","");

for($j=1; $j<=$length; $j++) {
if($_POST['question'][$j] != "") {
        $lik = $_POST['lik'];
        $likarr[$j] = explode("\n", str_replace("\r\n", "\n", $lik[$j]));
        $liko = $_POST['liko'];
        $likoarr[$j] = explode("\n", str_replace("\r\n", "\n", $liko[$j]));
        $ln = 1;
        for($lo=0;$lo<sizeof($likoarr[$j]);$lo++) {
            $val = $lo + 1;
            $likoprint[$j] .= '<td>'.$likoarr[$j][$lo].'</td>';
            $radiooptions[$j] .= '<td><input type="radio" name="likert'.$j.$lo.'" value="'.$val.'"></td>';
        }
        for($l=0;$l<sizeof($likarr[$j]);$l++) {
        $tablehead = '<td>Items\Options</td>'.$likoprint[$j];
        $tablerow[$j] .= '<tr><td>'.$likarr[$j][$l].'</td>'.$radiooptions[$j].'</tr>';
        $table = '<table style="width:900px;"><tr>'.$tablehead.'</tr>'.$tablerow[$j].'</table>';
}
        $bla .= $j.') '.$question[$j].'<br/>'.$table.'<br/><br/>';
}}

echo $bla;
?>

1 个答案:

答案 0 :(得分:0)

您正在获取无线电值,但这不是最佳方式。您可以做的是对问题的每个答案(选项)使用一个输入名称,并在输入值中使用该选项的值。这样您就可以轻松检查选项。像这样:

for($lo=0;$lo<sizeof($likoarr[$j]);$lo++) {
    $val = $lo + 1;
    $likoprint[$j] .= '<td>'.$likoarr[$j][$lo].'</td>';
    $radiooptions[$j] .= '<td><input type="radio" name="likert'.$j.'" value="'.$val.'"></td>';
}

要访问多个选项,请使用复选框,因为它可以轻松实现,选中和取消选中:

for($lo=0;$lo<sizeof($likoarr[$j]);$lo++) {
    $val = $lo + 1;
    $likoprint[$j] .= '<td>'.$likoarr[$j][$lo].'</td>';
    $radiooptions[$j] .= '<td><input type="checkbox" name="likert['.$j.'][]" value="'.$val.'"></td>';
}