1

我希望$order每次按下提交按钮时变量都会增加 1。

(页面名称是 study.php,所以每次我按下按钮都会刷新页面):

 <?php 
 $order = $_GET['number']; 

 echo "<form action='study.php' method='GET'>
   <input type='hidden' name='number' value='$order++' />
   <input class='big_b' type='submit'  value='next' /> 
 </form>";

 echo "$order";
 ?>

第一次 1 按下按钮 $order 是 1,第二次是 2,第三次是 3 ... 等等

谢谢你的帮助!

编辑:已解决

 <?php 
 session_start();
 if(empty($_SESSION['count'])) $_SESSION['count'] = 0;
 $order =  $_SESSION['count']+1;
 $_SESSION['count'] =  $order; 

 echo "<form action='study.php' method='GET'>
        <input class='big_b' type='submit'  value='next' /> 
    </form>";

 echo "$order";
 ?>
4

3 回答 3

1

您目前如何拥有它,无论是否单击按钮,它都会在每次页面刷新时递增,您是否只需要在按下按钮时递增?

<?php
session_start();

// Reset to 1
if(isset($_POST['reset'])){unset($_SESSION['number']);}

// Set or increment session number only if button is clicked.
if(empty($_SESSION['number'])){
    $_SESSION['number']=1;
}elseif(isset($_POST['next'])){
    $_SESSION['number']++;
}

echo '
<form action="" method="POST">
   <input class="big_b" type="submit" name="next" value="Next" /> 
   <input type="submit" name="reset" value="Reset" /> 
</form>';

echo $_SESSION['number'];
?>
于 2012-11-03T13:26:50.827 回答
0

您需要放在$order++引号之外进行操作(以 1 递增)。这是代码:

<?php 
 $order = $_GET['number']; 

 echo "<form action='study.php' method='GET'>
   <input type='hidden' name='number' value='".$order++."' />
   <input class='big_b' type='submit'  value='next' /> 
 </form>";

 echo "$order";
 ?>
于 2012-11-03T12:30:51.400 回答
0
<?php
session_start();
if(empty($_SESSION['order'])){

    $_SESSION['order'] = 1; 
}


 echo "<form action='study.php' method='GET'>
   <input type='hidden' name='number' value='".$_SESSION['order']++."' />
   <input class='big_b' type='submit'  value='next' /> 
 </form>";

 echo $_SESSION['order'];

?>
于 2012-11-03T12:44:56.367 回答