Jump to content

INCREASE VALUE BY ONE


baho

Recommended Posts

Hello everybody, if only you could help me to solve the problem below.....
Well, I have such code:

[color=green]
<html><body>
<?php
static $i=0;
?>
<form>
<input type=button name=button value="CLICK">
</form>
</body></html>[/color]

How can i increase the value of $i by 1, every time when I click the button.
....I would be very much pleased!!!
Link to comment
https://forums.phpfreaks.com/topic/16479-increase-value-by-one/
Share on other sites

To have PHP do this, you would have to submit the form and refresh the page each time the button was clicked to increase your counter. You would also have to include the current value of [b]$static[/b] as a hidden element in your form:

[code=php:0]
<?php

// Get the current value of $static
if(isset($_POST['static'])){
    $static = $_POST['static'];
} else {
    $static = 0;
}

// Check if the form was submitted
if($_POST['button']){
    $static++;
}
?>

<html><body>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="static" value="<?php echo $static; ?>">
<input type="submit" name="button" value="CLICK">
</form>
</body></html>
[/code]

As already mentioned, another option would be javascript. Create an "OnClick()" event that will increment the variable each time.
Link to comment
https://forums.phpfreaks.com/topic/16479-increase-value-by-one/#findComment-68757
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.