简体   繁体   中英

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):

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

$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:

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?

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. The $_POST holds the value of the radio input that has the checked tag.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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