1

I'd like that the variable $order is increase by 1 every time I push the submit button.

(The name of the page is study.php so every time I push the button the page is refreshed):

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

The first time 1 push the button $order is 1, the second 2, the third is 3 ... etc

Thanks for your help!

EDIT: SOLVED

 <?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";
 ?>
Shahrokhian
  • 1,100
  • 13
  • 28
Gago Design
  • 982
  • 2
  • 9
  • 16

3 Answers3

1

How you currently have it, is that it will increment on each page refresh regardless whether button was clicked or not, do you need it only to increment if the button is pressed?

<?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'];
?>
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
0

You need to put $order++ outside of the quotation marks to make an operation (incrementing by 1). Here's the code:

<?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";
 ?>
rationalboss
  • 5,330
  • 3
  • 30
  • 50
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'];

?>
Sibu
  • 4,609
  • 2
  • 26
  • 38