0

So this is probably not that difficult, but I still can't figure it out.

So I have a variable in php, called $number, and I have a submit button:

<form method="get"><button type="submit" value="<?php echo $number; ?> name="next">Next</button></form>.

What I'd like to achieve, is to increase the value of that variable, by 10 for example, every time the form is submitted.

First this is how I'd tried it:

if(isset($_GET['next'])){
$number+=10;
}

But this code only increases it's value once.

I've tried several methods that are not really worth mentioning, but none of them worked unfortunately.

I'd appreciate if someone could help me!

K. P.
  • 529
  • 4
  • 15
  • 3
    Every time the form is submitted per user, or any user? Assuming the latter, you need to store the number in a data store, whether that's a file on the server, a database, or something else. – Jonnix May 01 '19 at 12:11
  • 1
    pass last value in url and increment it with 1 – PHP Ninja May 01 '19 at 12:11

1 Answers1

2
<?php
session_start();
if(isset($_POST['next'])){
   $_SESSION['number'] += 10;
}
?>    
<form method="POST">
  <button type="submit" value="<?php echo $_SESSION['number']; ?>" name="next">
    Next
  </button>
</form>

POST the form, and use Sessions to ensure you still have the previous variable available after the reload.

Useful reading:

PHP Session: https://www.php.net/manual/en/reserved.variables.session.php

PHP Session Start: https://www.php.net/manual/en/function.session-start.php

According to Wikipedia:

GET requests a representation of the specified resource. Note that GET should not be used for operations that cause side-effects, such as using it for taking actions in web applications. One reason for this is that GET may be used arbitrarily by robots or crawlers, which should not need to consider the side effects that a request should cause.

and

POST submits data to be processed (e.g., from an HTML form) to the identified resource. The data is included in the body of the request. This may result in the creation of a new resource or the updates of existing resources or both.

Dammeul
  • 1,209
  • 11
  • 20