简体   繁体   English

我试图在页面之间传递变量,但我失败了

[英]I am trying to pass variables between pages and i am failing

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. 我一直试图在页面之间传递变量,以便在测验中计分,出于某种原因,变量没有按我希望的那样在页面之间传递,如果第一个按钮,我希望分数为1被击中,如果第二个被击中则为2,从现在开始,我击中的任何一个结果始终为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. 我很感谢您的帮助,如果这很愚蠢,对不起,我只是在学习如何编写html和php。

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; ?>" >

Place your 1st page php code to second page php on top. 将您的第一页php代码放置到第二页php的顶部。 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>

FirstPage.php should like below. FirstPage.php应该如下所示。

<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 SecondPage.php应该这样

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

            if ( isset( $_POST['Submit1'] ) ) { 
                $Score=$_POST['Submit1'];   
            }
            echo $Score; 
      ?>
    </p>
</body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM