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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀