我正在尝试为我的调查做一个插入语句,但首先是radiobuttons没有显示我认为脚本部分无法正常工作。这是一份调查表,我的目标是提交一份调查表。
已编辑(我认为脚本部分无效)
<div style="padding: 40px;">
<iframe name="votar" style="display:none;"></iframe>
<?php
$con = mysqli_connect("localhost","root","","imetrics");
$id = $_REQUEST['survey_id'];
$count=1;
$answer=0;
$query = "SELECT SF.survey_id, SF.surveytitle, SF.surveycategory, C.categoryname, Q.Option_1, Q.Option_2, Q.Option_3, Q.Option_4, Q.Option_5, Q.Option_6, Q.Option_7, Q.Option_8, Q.Option_9, Q.Option_10 AS pcatname, SF.surveysubcategory, SC.categoryname AS scatname, SF.gender, SF.age, SF.occupation, O.name, SF.occupation_status, SF.status, SFQ.question_id, Q.questiontitle
FROM surveyform AS SF
LEFT JOIN category AS C ON SF.surveycategory = C.category_id
LEFT JOIN category AS SC ON SF.surveysubcategory = SC.category_id
INNER JOIN occupation AS O ON SF.occupation = O.occupation_id
INNER JOIN surveyform_questions AS SFQ ON SF.survey_id = SFQ.survey_id
INNER JOIN question AS Q ON SFQ.question_id = Q.question_id
WHERE SFQ.survey_id = $id";
$result = mysqli_query($con,$query);
if($result) {
?>
<form method="POST" action="insertsurvey.php" target="votar">
<?php
while ($row = mysqli_fetch_array($result)) {
$questions = $row['questiontitle'];
echo "<b>Question " . $count . ".)</b> " . $questions . "</br>";
$option1 = $row['Option_1'];
$option2 = $row['Option_2'];
$option3 = $row['Option_3'];
$option4 = $row['Option_4'];
$option5 = $row['Option_5'];
$option6 = $row['Option_6'];
$option7 = $row['Option_7'];
$option8 = $row['Option_8'];
$option9 = $row['Option_9'];
$option10 = $row['Option_10'];
if (!is_null($option1)) {
?>
<div id="question_<?php echo $count; ?>_option1" style="display: none;">
<input type="radio" name="answer[<?php echo $count; ?>]" value="<?php echo $option1; ?>"> <?php echo $option1; ?>
</div>
<br>
<?php
}
if (!is_null($option2)) {
?>
<div id="question_<?php echo $count; ?>_option2" style="display: none;">
<input type="radio" name="answer[<?php echo $count; ?>]"
value="<?php echo $option2; ?>"> <?php echo $option2; ?>
</div>
<br>
<?php
}
if (!is_null($option3)) {
?>
<div id="question_<?php echo $count; ?>_option3" style="display: none;">
<input type="radio" name="answer[<?php echo $count; ?>]"
value="<?php echo $option3; ?>"> <?php echo $option3; ?>
</div>
<br>
<?php }
if (!is_null($option4)) {
?>
<div id="question_<?php echo $count; ?>_option4" style="display: none;">
<input type="radio" name="answer[<?php echo $count; ?>]"
value="<?php echo $option4; ?>"> <?php echo $option4; ?>
</div>
<br>
<?php }
if (!is_null($option5)) {
?>
<div id="question_<?php echo $count; ?>_option5" style="display: none;">
<input type="radio" name="answer[<?php echo $count; ?>]"
value="<?php echo $option5; ?>"> <?php echo $option5; ?>
</div>
<br>
<?php }
if (!is_null($option6)) {
?>
<div id="question_<?php echo $count; ?>_option6" style="display: none;">
<input type="radio" name="answer[<?php echo $count; ?>]"
value="<?php echo $option6; ?>"> <?php echo $option6; ?>
</div>
<br>
<?php }
if (!is_null($option7)) {
?>
<div id="question_<?php echo $count; ?>_option7" style="display: none;">
<input type="radio" name="answer[<?php echo $count; ?>]"
value="<?php echo $option7; ?>"> <?php echo $option7; ?>
</div>
<br>
<?php }
if (!is_null($option8)) {
?>
<div id="question_<?php echo $count; ?>_option8" style="display: none;">
<input type="radio" name="answer[<?php echo $count; ?>]"
value="<?php echo $option8; ?>"> <?php echo $option8; ?>
</div>
<br>
<?php }
if (!is_null($option9)) {
?>
<div id="question_<?php echo $count; ?>_option9" style="display: none;">
<input type="radio" name="answer[<?php echo $count; ?>]"
value="<?php echo $option9; ?>"> <?php echo $option9; ?>
</div>
<br>
<?php }
if (!is_null($option10)) {
?>
<div id="question_<?php echo $count; ?>_option10" style="display: none;">
<input type="radio" name="answer[<?php echo $count; ?>]"
value="<?php echo $option10; ?>"> <?php echo $option10; ?>
</div>
<br>
<?php }
$count++;
} ?>
<input type="submit" name="submit" value="submit"/>
</form>
<script src="//code.jquery.com/jquery-1.12.4.js"></script>
<script type="text/javascript">
jQuery(function($) {
var id1 = <?php echo json_encode($option1); ?>;
var id2 = <?php echo json_encode($option2); ?>;
var id3 = <?php echo json_encode($option3); ?>;
var id4 = <?php echo json_encode($option4); ?>;
var id5 = <?php echo json_encode($option5); ?>;
var id6 = <?php echo json_encode($option6); ?>;
var id7 = <?php echo json_encode($option7); ?>;
var id8 = <?php echo json_encode($option8); ?>;
var id9 = <?php echo json_encode($option9); ?>;
var id10 = <?php echo json_encode($option10); ?>;
if (id1 != null) {
$("div#option1").show();
}
if (id2 != null) {
$("div#option2").show();
}
if (id3 != null) {
$("div#option3").show();
}
if (id4 != null) {
$("div#option4").show();
}
if (id5 != null) {
$("div#option5").show();
}
if (id6 != null) {
$("div#option6").show();
}
if (id7 != null) {
$("div#option7").show();
}
if (id8 != null) {
$("div#option8").show();
}
if (id9 != null) {
$("div#option9").show();
}
if (id10 != null) {
$("div#option10").show();
}
})(jQuery);
</script>
</div>
</div>
</body>
</html>
<?php
}
?>
what my survey form looks like What the script part looks like
答案 0 :(得分:1)
问题(除了Xorifelse提到的SQL注入问题)与您的HTML有关。每个问题的name
属性需要不同。拥有它是相同的是您的单选按钮的行为方式。
你可以这样做(有很多方法可以解决这个问题):
<input type="radio" name="answer[<?php echo $count; ?>]" value="<?php echo $value; ?>"> <?php echo $value; ?>
当你这样做时,PHP会(一般来说......根据服务器配置可能会有例外情况)把它放到一个数组中,如:
$_REQUEST['answer'] => array(
1 => 'answer to question 1',
2 => 'answer to question 2',
etc
);
您应该注意,在您执行的地方增加$count
变量将有效地“跳过”第一个问题,因此您应将其移至while () { }
块的末尾:
while (condition) {
...
$count++;
}
哦,关于你的提交按钮问题,只需将它放在while
循环之外就可以了:
<?php
while (condition) {
// do all your stuff
$count++;
}
?>
<input type="submit" name="submit" value="submit" />
</form>
编辑以下内容以获得更完整的示例。
请注意:您正在通过$ option10显式引用$ option1只是乞求进行循环,但我已将其保留为清晰度,因此更容易遵循。这不是我真正做的,但它最接近你的原始代码,以便于理解。
<?php
// all your same stuff before this mysqli_query call...
$result = mysqli_query($con,$query);
if ($result) {
?>
<form action="php-file.php" method="post">
<?php
while ($row = mysqli_fetch_array($result)) {
$questions = $row['questiontitle'];
echo "<b>Question " . $count . "</b> " . $questions . "<br>";
$option1 = $row['Option_1'];
$option2 = $row['Option_2'];
$option3 = $row['Option_3'];
$option4 = $row['Option_4'];
$option5 = $row['Option_5'];
$option6 = $row['Option_6'];
$option7 = $row['Option_7'];
$option8 = $row['Option_8'];
$option9 = $row['Option_9'];
$option10 = $row['Option_10'];
if (!is_null($option1)) {
?>
<div id="question_<?php echo $count; ?>_option1" style="display: none;">
<input type="radio" name="answer[<?php echo $count; ?>]" value="<?php echo $option1; ?>"> <?php echo $option1; ?>
</div>
<br>
<?php
} // if
if (!is_null($option2)) {
?>
<div id="question_<?php echo $count; ?>_option2" style="display: none;">
<input type="radio" name="answer[<?php echo $count; ?>]" value="<?php echo $option2; ?>"> <?php echo $option2; ?>
</div>
<br>
<?php
} // if
if (!is_null($option3)) {
?>
<div id="question_<?php echo $count; ?>_option3" style="display: none;">
<input type="radio" name="answer[<?php echo $count; ?>]" value="<?php echo $option3; ?>"> <?php echo $option3; ?>
</div>
<br>
<?php
} // if
// options 4-9 here, the exact same way as option 1-3 above
if (!is_null($option10)) {
?>
<div id="question_<?php echo $count; ?>_option10" style="display: none;">
<input type="radio" name="answer[<?php echo $count; ?>]" value="<?php echo $option10; ?>"> <?php echo $option10; ?>
</div>
<br>
<?php
} // if
$count++;
} // end while
?>
<input type="submit" name="submit" value="submit" />
</form>
<?php
} // end if (result)
以下是您的解决方案可能(我认为)。请注意,我已使用循环替换了显式$optionX
定义。你不需要javascript的原因是因为你已经用PHP检查选项是否存在,所以你应该能够安全地显示它 - 没有display: hidden;
样式。
<?php
# this stuff removed for brevity, but you'd just put the same stuff you had originally
if ($result) {
?>
<form method="POST" action="insertsurvey.php" target="votar">
<?php
while ($row = mysqli_fetch_array($result)) {
$questions = $row['questiontitle'];
echo "<b>Question " . $count . ".)</b> " . $questions . "<br>";
for ($i = 1; $i <= 10; $i++) {
if (!empty($row['Option_' . $i])) {
?>
<div id="question_<?php echo $count; ?>_option<?php echo $i; ?>">
<input type="radio" name="answer[<?php echo $count; ?>]" value="<?php echo $row['Option_' . $i]; ?>"> <?php echo $row['Option_' . $i]; ?>
</div>
<?php
}
}
$count++;
}
?>
<input type="submit" name="submit" value="submit"/>
</form>
<!-- no javascript/jquery script needed anymore; though obviously you'd want to restore it if you have javascript doing something else -->
<?php
}
?>