Php 如何获得多项选择题调查的答案?
我正在做一个调查网站,现在我正在努力回答多项选择题。一切正常,但现在我想访问用户选择的答案,但我不知道如何准确地做到这一点 我的初始代码看起来像ThatAnswing.php:Php 如何获得多项选择题调查的答案?,php,html,mysql,Php,Html,Mysql,我正在做一个调查网站,现在我正在努力回答多项选择题。一切正常,但现在我想访问用户选择的答案,但我不知道如何准确地做到这一点 我的初始代码看起来像ThatAnswing.php: <?php include_once 'init/init.funcs.php'; $_SESSION['pollid']=(int) $_GET['pollid']; $questions = array(); if (!isset($_SESSION['answering'])) { $result =
<?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']++;
?>
然后为了控制它是否有效,我在第二个文件中添加了以下代码:
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']++;
?>
但当我改变以下部分时:
<input type="radio" name="option" value=<?php $option ?>><?php echo $option?><br>
比如说:
如果我回答前面的问题,它会在问题的开头打印出dog
问题似乎是我的单选按钮没有收到选项的值,如何解决这个问题?简单添加更多的单选输入
<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中执行以下操作。$\u 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.
编辑示例
$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'>";
}
}
对于新的单选$\u POST input更改名称标记,代码将创建尽可能多的单选按钮,因为数据库中当前问题可能有答案。我不想手动添加单选按钮。然后对可能的答案使用foreach循环,并为每个答案回显一个单选按钮,值=$answer。还可以使用一些增量来更改名称。每个问题。你能写一些例子吗?是的,等等,我得深入研究一下
<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">
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'>";
}
}