dshgdhdfcas30210 2014-04-23 08:16
浏览 166
已采纳

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

This question will probably be marked as duplicate, but I didnt find other answers helpful.

I am creating a site where user could answer multiple type of questions. Thus i wrote code so the corresponding html template will appear, according to type of question. Right now my php code is following:

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

And what I want to do is to make following html template appear, when $type=='1'.

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

My question is, how could I make it appear with question. Right now $_SESSION['answering']['questions'][$x]; is undefined.How could I transfer it from first file to second?

  • 写回答

3条回答 默认 最新

  • doumei7420 2014-04-23 08:27
    关注

    $_SESSION['answering']['questions'];
    contains a list of items, you have to specify which one should be displayed.

    In case you need the last index, this is simple (and ugly) solution:

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

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP