我正在尝试创建动态调查,因为用户/管理员可以创建在从应用程序回答调查时出现的问题,而不是硬编码。
到目前为止我的代码:
<div class="surveybox">
<ol>
<?php
$result = mysqli_query($db, "SELECT QuestionGroup FROM surveyquestions");
$unique = array();
while ($row = $result->fetch_assoc()) {
$header = $row["QuestionGroup"];
$unique[] = $header;
}
foreach (array_unique($unique) as $name) {
$questionsget = mysqli_query($db, "SELECT * FROM surveyquestions WHERE QuestionGroup = '$name'") or die(mysql_error());
echo('<form action="GET" method="storesurveyresponse.php">');
echo '<div class="questionheader"><h2>' . $name . '</h2></div><br>';
while ($row = $questionsget->fetch_assoc()) {
$id = $row["QuestionID"];
$questiongroup = $row["QuestionGroup"];
$questiontext = $row["Question"];
$responsefield = $row["ResponseField"];
$questionID = $row["QuestionID"];
echo '<div class="questionbody"><li>' . $questiontext . '</li><br><br>';
if ($responsefield == "Radio Button") {
echo '<input type="radio" name="' . $questionID . '" value="Excellent">Excellent<br>
<input type="radio" name="radio' . $questionID . '" value="Good">Good<br>
<input type="radio" name="radio' . $questionID . '" value="Average">Average<br>
<input type="radio" name="radio' . $questionID . '" value="Good">Below Average<br>
<input type="radio" name="radio' . $questionID . '" value="Poor">Poor ';
$answer = $_GET["radio".$questionID]; //filter_input(INPUT_POST, 'radio' . $questionID);
} elseif ($responsefield == "Comment Box") {
echo '<textarea class="questionarea" name="question" placeholder="Enter Answer here..."></textarea></div>';
}
echo '<div class="responsebutton"> <button class="submit"><a class="button" href="storesurveyresponse.php?ownerID=0&questionID=' . $questionID . '&questiontext=' . $questiontext . '&questiongroup=' . $questiongroup . '&responsefield=' . $responsefield . '&answer=' . $answer . '"=>Submit</a></button></div><br><br><br><br></div>';
echo'</form>';
}
}
?>
</ol>
</div>
上面的代码在从数据库中布置问题并在提交时插入所有其他所需的数据库行值时非常有效,除了所选的单选按钮值。单选按钮值列始终为空白。 在加载的所有问题下,我还在页面加载“注意:未定义的索引:C中的radio38:第52行的C:\ xampp \ htdocs ....”中得到以下错误,但是我提交了一个回复后错误消失了问题,我不知道它们是否相关。
如您所见,我使用$ _GET方法提交值,我的处理代码如下:
<?php
include 'session.php';
$ownerID = filter_input(INPUT_GET, ownerID);
$questionsID = filter_input(INPUT_GET, questionID);
$questionsText = filter_input(INPUT_GET, questiontext);
$questionsgroup = filter_input(INPUT_GET, questiongroup);
$replyfield = filter_input(INPUT_GET, responsefield);
$answer = filter_input(INPUT_GET, answer);
$date = date("Y/m/d");
mysqli_query($db, "INSERT INTO surveyanswers
(OwnerID, UserID,ParticipantName, DateStart,
DateEnd, QuestionID, QuestionText, QuestionGroup,
ResponseField, Answer)
VALUES ('$ownerID', 'aaa', 'aaa', '$date',
'$date','$questionsID', '$questionsText', '$questionsgroup',
'$replyfield','$answer')")or die(mysqli_error($db));
header("Location: takesurvey.php");
我查看了相关问题并在网上搜索过,我无法找到任何具体问题/示例来解决这个问题。
答案 0 :(得分:0)
我做了一些改动,最后根据你的指示和建议让它工作。我将get方法添加到表单声明中,并将处理代码放在与表单相同的页面上。
以下是代码:
foreach (array_unique($unique) as $name) {
$questionsget = mysqli_query($db, "SELECT * FROM surveyquestions WHERE QuestionGroup = '$name'") or die(mysql_error());
echo '<div class="questionheader"><h2>' . $name . '</h2></div><br>';
while ($row = $questionsget->fetch_assoc()) {
$id = $row["QuestionID"];
$questiongroup = $row["QuestionGroup"];
$questiontext = $row["Question"];
$responsefield = $row["ResponseField"];
$questionID = $row["QuestionID"];
echo '<form action="" method="get">';
echo '<div class="questionbody"><li>' . $questiontext . '</li><br><br>';
if ($responsefield == "Radio Button") {
echo '<input type="radio" name="radio'. $questionID . '" value="Excellent">Excellent<br>
<input type="radio" name="radio' . $questionID . '" value="Good">Good<br>
<input type="radio" name="radio' . $questionID . '" value="Average">Average<br>
<input type="radio" name="radio' . $questionID . '" value="Good">Below Average<br>
<input type="radio" name="radio' . $questionID . '" value="Poor">Poor ';
$answer = "radio" . $questionID; //filter_input(INPUT_POST, 'radio' . $questionID);
echo '<input type="submit" name="submit" value="Submit">';
if (isset($_GET['submit'])) {
if (isset($_GET['radio' . $questionID])) {
$answer = filter_input(INPUT_GET, 'radio' . $questionID);
$ownerID = "aaa";
$questionsID = $questionID;
$questionsText = $questiontext;
$questionsgroup = $questiongroup;
$replyfield = $responsefield;
$date = date("Y/m/d");
mysqli_query($db, "INSERT INTO surveyanswers(OwnerID, UserID, ParticipantName, DateStart, DateEnd, QuestionID, QuestionText, QuestionGroup, ResponseField, Answer) VALUES ('$ownerID', 'aaa', 'aaa', '$date', '$date', '$questionsID', '$questionsText', '$questionsgroup', '$replyfield', '$answer')")or die(mysqli_error($db));
}
}
} elseif ($responsefield == "Comment Box") {
echo '<textarea class="questionarea" name="question" placeholder="Enter Answer here..."></textarea></div>';
}
echo '</form>';
}
}
谢谢!