Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.