douqiaoru2583 2018-03-16 20:06
浏览 56
已采纳

在php页面中传递会话值的问题

i have quizaction.php page and result.php page. I am passing variables from quizaction.php to result.php. this is my quizaction.php:

    <?php

      include 'db.php';
      session_start();
    ?>
    <html>
    <head><title>Quiz Feedback</title></head>
    <body bgcolor="#FFFFFF">
    <form name="result" action=result.php  method="POST"> 
      <?php
      $_SESSION['time_left'] = $_POST['formvar'];
      $_SESSION['time_taken'] = 60 - $_SESSION['time_left'];
      ?>
     <INPUT TYPE="SUBMIT" VALUE="Done" >
    </form>
    </body>
    </html>

and my result.php is:

    <?php 
      session_start();
      include 'db.php';
      include 'quizaction.php';
    ?>
    <button onclick="myFunction()">Try it</button>

    <script type="text/javascript">
        function myFunction() {

          var topic1= "<?php echo $type[0]; ?>";
          var topic2= "<?php echo $type[1]; ?>";

          var score= "<?php echo ($score); ?>";
          alert("check");
          alert(score); 
          alert(topic1);
          alert(topic2);
       }
       </script>

but im unable to retrieve value of score from quizaction.php. Actually im getting data in quizaction.php also from other page. But here i dont think there is any problem with that. Please help me out. Even expatiation will work

  • 写回答

2条回答 默认 最新

  • dongmimeng5500 2018-03-16 23:13
    关注

    This script has a lot of issues:

    1) In the result.php page, you have that html output coming after the page has already rendered the contents of quizaction.php which contains the start and end tags of <html>. That is not idea.

    2) When including one into the other, you have two session_start(), which will cause an warning/error. That is not ideal.

    3) In result.php you have $type and $score defined, but it's nowhere in either page posted in your sample script.

    I think your main problem is organizational. You probably had things working individually and are trying to squeeze them into each other now, which is not the best way to do it. You have too much duplication and duplication means you introduce inconsistency and potential for error. Down the road, you will have a hard time maintaining this.

    If possible, use some sort of framework. If that is not something you can do, re-organize your script(s). I would:

    1) Make a config file that contains your database, session, and other easy-reference items:

    /config.php

    <?php
    define('DS',DIRECTORY_SEPARATOR);
    define('ROOT_DIR',__DIR__);
    define('FUNCTIONS',ROOT_DIR.DS.'functions');
    # Start session
    session_start();
    # Include database
    require_once('db.php');
    

    2) You have to define $type and $score, I don't know where that is coming from so you have to figure out where that is coming from. Based on your scripts posted, you only have $_SESSION and $_POST defined.

    /result.php

    <button onclick="myFunction()">Try it</button>
    
    <script type="text/javascript">
        function myFunction()
        {
            var topic1= "<?php echo $type[0] ?>";
            var topic2= "<?php echo $type[1] ?>";
            var score = "<?php echo $score ?>";
    
            alert("check");
            alert(score); 
            alert(topic1);
            alert(topic2);
        }
    </script>
    

    3) Include result.php into the quiz page instead of the other way around.

    /quizaction.php

    <?php
    # Include the config at the top of the main pages
    require_once(__DIR__.DIRECTORY_SEPARATOR.'config.php');
    # Move non-view logic before view
    # Check if something is set first
    if(!empty($_POST['formvar'])) {
        $_SESSION['time_left']  = $_POST['formvar'];
        $_SESSION['time_taken'] = 60 - $_SESSION['time_left'];
    }
    ?>
    <html>
    <head><title>Quiz Feedback</title></head>
    <body bgcolor="#FFFFFF">
    <!-- Use quotes here ------v----------v -->
    <form name="result" action="result.php"  method="POST"> 
        <input type="submit" value="Done" >
    </form>
    <?php include(ROOT_DIR.DS.'result.php') ?>
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题