我正在制作一个调查网站,现在我正在努力回答多项选择题。一切正常,但现在我想访问用户选择的答案,但我不知道如何做到这一点。
我的初始代码看起来像那样(answersering.php):
<?php
include_once 'init/init.funcs.php';
$_SESSION['pollid']=(int) $_GET['pollid'];
$questions = array();
if (!isset($_SESSION['answering'])) {
$result = mysql_query('SELECT * from katse_kysimused where kysimustik_id="' . $_SESSION['pollid'] . '"');
while($row = mysql_fetch_assoc($result)) {
$questions[] = $row['kysimus'];
}
$_SESSION['answering']['questions'] = $questions;
$_SESSION['answering']['index'] = 0;
}
$x = $_SESSION['answering']['index'];
$result3 = mysql_query('SELECT tyyp_id FROM katse_kysimused where kysimus= "' . $_SESSION['answering']['questions'][$x] . '"');
$type = mysql_result($result3, 0);
if ($type=='3'){
echo "<meta http-equiv='refresh' content='0;url=http://localhost/Praks/valikvastustega_kysimusele_vastamine.php'>";
}
if ($type=='1'){
echo "<meta http-equiv='refresh' content='0;url=http://localhost/Praks/tekstkysimusele_vastamine2.php'>";
$answer = $_POST['answer'];
}
if(isset($_POST['submit'])){
$result2 = mysql_query('SELECT kysimus_id FROM katse_kysimused where kysimus= "' . $_SESSION['answering']['questions'][$x -1] . '"');
$q_id = mysql_result($result2, 0);
mysql_query('INSERT INTO katse_vastused2 (id, vastus,kysimus_id, vastustik_id) VALUES (NULL,"' . $answer . '","' . $q_id . '","1")');
}
$_SESSION['answering']['index']++;
?>
代码的其他部分正在跟随(它用于显示多项选择题):
<?php
include_once 'init/init.funcs.php';
$x = $_SESSION['answering']['index'];
echo $_SESSION['answering']['questions'][$x-1];
$result4 = mysql_query('SELECT kysimus_id FROM katse_kysimused where kysimus= "' . $_SESSION['answering']['questions'][$x-1] . '"');
$question_id = mysql_result($result4, 0);
$result5 = mysql_query('SELECT * from katse_valik_vastused where kysimus_id="' . $question_id . '"');
if($result5 === FALSE) {
die(mysql_error());
}
while($row = mysql_fetch_assoc($result5)) {
$options[] = $row['vasuts'];
}
?>
<html>
<br>
<form method="post" action="answering.php">
<?php
foreach($options as $option=>$option_value) {
?>
<input type="radio" name="option" value=<?$option_value?>><?php echo $option_value?><br>
<?php }?>
<input name= "submit" type="submit" value="Vasta">
</form>
为了获得答案,我尝试了以下用户提供的答案。我将以下代码添加到我的第一个文件(answering.php)中。
$selected_radio = $_POST['option'];
$_SESSION['answering']['selected_radio'] = $selected_radio;
然后控制它是否有效,我将以下代码添加到我的第二个文件中:
print $_SESSION['answering']['selected_radio'];
但它没有用。我应该写什么以及在哪里访问多项选择题的答案?
编辑:
现在我的两段代码看起来像这样:
<?php
include_once 'init/init.funcs.php';
$_SESSION['pollid']=(int) $_GET['pollid'];
$questions = array();
if (!isset($_SESSION['answering'])) {
$result = mysql_query('SELECT * from katse_kysimused where kysimustik_id="' . $_SESSION['pollid'] . '"');
while($row = mysql_fetch_assoc($result)) {
$questions[] = $row['kysimus'];
}
$_SESSION['answering']['questions'] = $questions;
$_SESSION['answering']['index'] = 0;
}
$x = $_SESSION['answering']['index'];
$result3 = mysql_query('SELECT tyyp_id FROM katse_kysimused where kysimus= "' . $_SESSION['answering']['questions'][$x] . '"');
$type = mysql_result($result3, 0);
if ($type=='3'){
echo "<meta http-equiv='refresh' content='0;url=http://localhost/Praks/valikvastustega_kysimusele_vastamine.php'>";
}
if ($type=='1'){
echo "<meta http-equiv='refresh' content='0;url=http://localhost/Praks/tekstkysimusele_vastamine2.php'>";
$answer = $_POST['answer'];
}
if(isset($_POST['submit'])){
$result2 = mysql_query('SELECT kysimus_id FROM katse_kysimused where kysimus= "' . $_SESSION['answering']['questions'][$x -1] . '"');
$q_id = mysql_result($result2, 0);
mysql_query('INSERT INTO katse_vastused2 (id, vastus,kysimus_id, vastustik_id) VALUES (NULL,"' . $answer . '","' . $q_id . '","1")');
$selected_radio = $_POST['option'];
$_SESSION['answering']['option']=$selected_radio;
}
$_SESSION['answering']['index']++;
?>
和
<?php
include_once 'init/init.funcs.php';
$x = $_SESSION['answering']['index'];
echo $_SESSION['answering']['questions'][$x-1];
$result4 = mysql_query('SELECT kysimus_id FROM katse_kysimused where kysimus= "' . $_SESSION['answering']['questions'][$x-1] . '"');
$question_id = mysql_result($result4, 0);
$result5 = mysql_query('SELECT * from katse_valik_vastused where kysimus_id="' . $question_id . '"');
if($result5 === FALSE) {
die(mysql_error());
}
while($row = mysql_fetch_assoc($result5)) {
$options[] = $row['vasuts'];
}
?>
<html>
<?php
echo $_SESSION['answering']['option'];
?>
<br>
<form method="post" action="answering.php">
<?php
foreach($options as $option) {
?>
<input type="radio" name="option" value=<?php $option ?>><?php echo $option?><br>
<?php }?>
<input name= "submit" type="submit" value="Vasta">
</form>
一切似乎都很好,没有错误出现。但仍然遵循以下代码行不会打印出前一个问题的给定答案,它位于html部分开头的第二个文件中:
echo $_SESSION['answering']['option'];
但是当我改变以下部分时:
<input type="radio" name="option" value=<?php $option ?>><?php echo $option?><br>
来,让我们说:
如果我回答上一个问题,它会在问题开头打印出狗
问题似乎是我的单选按钮没有收到选项的价值,如何解决?
答案 0 :(得分:0)
简单添加更多无线电输入。
<input type="radio" name="option" value="value1"><br>
<input type="radio" name="option" value="value2"><br>
<input type="radio" name="option" value="value3"><!-- the value defines the choice. -->
如果您点击一个按钮它会像这样收到已检查的标签。
<input type="radio" name="option" value="value1" checked><br>//this one is clicked now
<input type="radio" name="option" value="value2"><br>
<input type="radio" name="option" value="value3">
然后在你的php中执行以下操作。 $ _POST保存具有checked标签的无线电输入的值。
if(isset($_POST["option"])){ //checks if any of the buttons is checked.
$var = $_POST["option"]; //this is now either value1, value2
//or value3 depending on which was selected.
}else{ //If there was no button selected.
echo "must select an option";
}
//$_POST['option'] <-- 'option' here is the name of the radio button.
//$var = $_POST['option'] <-- $var will now hold the value of the checked radio button.
编辑示例
$questions = array() //instert questions can be done automaticaly probably.
//for if the question names are not unique.
$i=0;
//for all the questions
foreach($questions as $x){
//Get all answers for the question put them in another array.
$answers = array();
$answers = $x->getanswers();
//if quesionnames are not unique an increment to make question names unique.
$x = $x.$i;
$i++;
//for all the answers of the found question
foreach($answers as $y){
//echo a radion buttion with the name of the question and the value of the answer.
echo "<input type='radio' name='$x' value='$y'>";
}
}