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

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?