0

I have been trying to pass a variable between pages so as to tally up a score on a quiz, for some reason the variables aren't passing between the pages as I want them to, I want the score to be 1 if the first button is hit, and it to be 2 if the second is hit, as of now which ever one I hit the result is always 0.

1st Page - minus a whole load of styling:

<?PHP

$Score=0;

if ( isset( $_POST['Submit1'] ) ) { 
    $Score=$Score+1;
}

if ( isset( $_POST['Submit2'] ) ) { 
    $Score=$Score+2;
}

?>

<body>

<center><img src="http://fc06.deviantart.net/fs70/f/2011/002/d/3/purple_blob_pet_by_bunni0222-d36aw4q.png" alt="" align="middle"/></center>

<FORM NAME ="form1" METHOD ="POST" ACTION ="Test1.php">

<INPUT TYPE = "Hidden" Name = "h1" Value = <?PHP echo $Score; ?> >
<center><INPUT TYPE = "Submit" class=myButton Name = "Submit1" VALUE = "1"></center>
<center><INPUT TYPE = "Submit" class=myButton Name = "Submit2" VALUE = "2"></center>

</FORM>


</body>

This is the complete second page:

<html>
  <head>

    <title>Test</title>

  </head>
  <body>
    <p>
      <?php
        $Score = 0;
        //error_reporting(0);
        $Score = $_POST['h1'];
        echo $Score;


      ?>
    </p>
  </body>
</html>

I would appreciate the help, if it is something stupid I apologise i am just learning how to write html and php.

3 Answers3

0

I think problem is in this line:

<INPUT TYPE = "Hidden" Name = "h1" Value = <?PHP echo $Score; ?> >

to

<INPUT TYPE = "Hidden" Name = "h1" Value = "<?PHP echo $Score; ?>" >
Lovepreet Singh
  • 4,792
  • 1
  • 18
  • 36
0

Place your 1st page php code to second page php on top. It will work..

1st page will be

<body>
<center><img src="http://fc06.deviantart.net/fs70/f/2011/002/d/3/purple_blob_pet_by_bunni0222-d36aw4q.png" alt="" align="middle"/></center>
<FORM NAME ="form1" METHOD ="POST" ACTION ="Test1.php">
<INPUT TYPE = "Hidden" Name = "h1" Value = <?PHP echo $Score; ?> >
<center><INPUT TYPE = "Submit" class=myButton Name = "Submit1" VALUE = "1"></center>
<center><INPUT TYPE = "Submit" class=myButton Name = "Submit2" VALUE = "2"></center>
</FORM>
</body>

and second page will be

<html>
  <head>
    <title>Test</title>
  </head>
  <body>
    <p>
      <?php
        $Score=0;
if ( isset( $_POST['Submit1'] ) ) { 
    $Score=$Score+1;
}

if ( isset( $_POST['Submit2'] ) ) { 
    $Score=$Score+2;
}
        $Score = $_POST['h1'];
        echo $Score; ?>
    </p>
  </body>
</html>
Anuranjan Pandey
  • 132
  • 2
  • 11
0

FirstPage.php should like below.

<body>

    <FORM NAME ="form1" METHOD ="POST" action="SecondPage.php">
        <center><input TYPE = "Submit"  name = "Submit1" VALUE = "1"></center>
        <center><input TYPE = "Submit" name = "Submit1" VALUE = "2"></center>
    </FORM>

</body>

SecondPage.php should like this

<body>
    <p>
      <?php
            $Score=0;   

            if ( isset( $_POST['Submit1'] ) ) { 
                $Score=$_POST['Submit1'];   
            }
            echo $Score; 
      ?>
    </p>
</body>
Jekin Kalariya
  • 3,475
  • 2
  • 20
  • 32