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

在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

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>

Add this to results.php

<?PHP PRINT_R($_POST); // For Testing ?>

quizaction.php
All Input fields must have names like

<INPUT TYPE=text VALUE='123' name='topic_1'>

To make something a session variable
- 1st session_start
- 2nd results.php

<?PHP $_SESSION['topic_1'] = $_POST['topic_1']; ?>

This should help you along to where you need to go.

</div>