如何将变量从一个php文件传输到另一个?

时间:2014-04-23 08:16:13

标签: php html mysql

这个问题可能会被标记为重复,但我没有找到其他有用的答案。

我正在创建一个用户可以回答多种问题的网站。因此我编写了代码,因此根据问题的类型将出现相应的html模板。现在我的PHP代码如下:

<?php
include_once 'init/init.funcs.php';
$_SESSION['pollid']=(int) $_GET['pollid'];
$questions = array();
$answer = $_POST['answer'];
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 $_SESSION['answering']['questions'][$x];
        $result4 = mysql_query('SELECT kysimus_id FROM katse_kysimused where kysimus= "' . $_SESSION['answering']['questions'][$x] . '"');
        $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'];
        }
        foreach($options as $option=>$option_value) {
            echo $option_value;
}
        }

    if ($type=='1'){
            echo "<meta http-equiv='refresh' content='0;url=http://localhost/Praks/tekstkysimusele_vastamine2.php'>";
        }
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']++;
?>

我想要做的是在$ type ==&#39; 1&#39;时出现以下html模板。

<?php
echo $_SESSION['answering']['questions'][$x];
?>
<html>
<br>
<form method="post" action="answering.php">
<input type="text" name="answer" style="width:300px; height:150px;"><br>
<input name= "submit" type="submit" value="Vasta">
</form>
</html>

我的问题是,我怎么能让它出现问题。现在$ _SESSION [&#39;回答&#39;] [&#39;问题&#39;] [$ x];未定义。如何将它从第一个文件传输到第二个文件?

3 个答案:

答案 0 :(得分:0)

删除$x,然后尝试:

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

答案 1 :(得分:0)

$_SESSION['answering']['questions'];
包含列表项目,您必须指定应显示哪一个。

如果您需要最后一个索引,这是一个简单(丑陋)的解决方案:

<?php
    $last_index = count($_SESSION['answering']['questions']) -1;
    echo $_SESSION['answering']['questions'][$last_index];
?>
<html>
<br>
<form method="post" action="answering.php">
<input type="text" name="answer" style="width:300px; height:150px;"><br>
<input name= "submit" type="submit" value="Vasta">
</form>
</html>

答案 2 :(得分:0)

我解决了这个问题。现在我的第二个文件是:

<?php
include_once 'init/init.funcs.php';
$x = $_SESSION['answering']['index'];
echo $_SESSION['answering']['questions'][$x];
?>
<html>
<br>
<form method="post" action="answering.php">
<input type="text" name="answer" style="width:300px; height:150px;"><br>
<input name= "submit" type="submit" value="Vasta">
</form>
</html>
相关问题