每次按下按钮都会增加一个变量(PHP)

时间:2012-11-03 12:28:11

标签: php

我希望每次按下提交按钮时变量$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";
 ?>

第一次按下按钮$ 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";
 ?>

3 个答案:

答案 0 :(得分: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'];
?>

答案 1 :(得分: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";
 ?>

答案 2 :(得分: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'];

?>