Jump to content

Archived

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

baho

INCREASE VALUE BY ONE

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!!!

Share this post


Link to post
Share on other sites
You cant without submitting the form. You cannot use PHP like you can with javascript. However you can if you use AJAX.

Share this post


Link to post
Share on other sites
He could use Javascript alone too, depending on his purpose.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
buttons dont submit forms. You'll want to use submit button and not a normal button.

Share this post


Link to post
Share on other sites
Ok,
thank you for advice, but is it possible to do it without submitting the form? Like using javascript. If yes, can you give me a javascript code for where php variable $i increases...

thanx.

Share this post


Link to post
Share on other sites
[code]
<script language="javascript">
<!---
static=0;
function Increment() {
static++;
}
//-->
</script>

<form>
<input type=button name=button value="CLICK" onClick="Increment()">
</form>
[/code]

Share this post


Link to post
Share on other sites

×

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.