简体   繁体   English

如何获得多项选择调查的答案?

[英]How to get answers of multiple choice survey?

I am making a survey site and right now I a working on answering to multiple choice questions. 我正在做一个调查站点,现在我正在回答多项选择题。 Everything works fine but now I want to access answers which user selects but I dont know how to exactly do it. 一切正常,但现在我想访问用户选择的答案,但我不知道该怎么做。

My initial code looked like that(answering.php): 我的初始代码如下所示(answering.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']++;
?>

And other part of code is following(it's for showing multiple choice questions): 代码的其他部分如下(用于显示选择题):

<?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>

For accessing answer, which user gave I tried following. 为了访问答案,我尝试了以下用户给的答案。 I added following code into my first file(answering.php). 我将以下代码添加到了我的第一个文件(answering.php)中。

$selected_radio = $_POST['option'];
$_SESSION['answering']['selected_radio'] =  $selected_radio;

And then to control if it worked, I added following code into my second file: 然后,为了控制它是否起作用,我在第二个文件中添加了以下代码:

print $_SESSION['answering']['selected_radio'];

But it didnt work. 但是它没有用。 What and where should I write to access answers of multiple choice questions? 我应该写什么地方写选择题答案?

EDIT: 编辑:

Right now my two pieces of code look like that: 现在,我的两段代码如下所示:

<?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']++;
?>

And

<?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>

Everything seems to work just fine, no errors appear. 一切似乎都正常,没有错误出现。 But still following line of code wont print out the given answer to previous question, it is in the second file at the beginning of html part: 但是仍然遵循下面的代码行不会打印出先前问题的给定答案,它位于html部分开头的第二个文件中:

echo $_SESSION['answering']['option'];

But when I change following part: 但是当我更改以下部分时:

<input type="radio" name="option" value=<?php $option ?>><?php echo $option?><br>

To, let's say: 到,说:

It prints out dog at the beginning of question, if I answer to previous question 如果我回答上一个问题,它将在问题的开头打印出狗

The problem seems to be that my radio button doesn't recieve the value of option, how to solve that? 问题似乎是我的单选按钮不接受option的值,如何解决呢?

simple add more radio inputs. 简单添加更多无线电输入。

<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. -->

If you click a button It will recieve the checked tag like so. 如果单击按钮,它将像这样接收已选中的标签。

<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">

then in your php do the following. 然后在您的PHP中执行以下操作。 The $_POST holds the value of the radio input that has the checked tag. $ _POST保存具有选中标记的单选输入的值。

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. 

edit example 编辑范例

$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'>";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM