Jump to content


Photo

INCREASE VALUE BY ONE


  • Please log in to reply
6 replies to this topic

#1 baho

baho
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 03 August 2006 - 06:23 PM

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


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


How can i increase the value of $i by 1, every time when I click the button.
....I would be very much pleased!!!


#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 03 August 2006 - 06:25 PM

You cant without submitting the form. You cannot use PHP like you can with javascript. However you can if you use AJAX.

#3 CTM

CTM
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 03 August 2006 - 06:26 PM

He could use Javascript alone too, depending on his purpose.

#4 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 03 August 2006 - 06:32 PM

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 $static as a hidden element in your form:

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

As already mentioned, another option would be javascript. Create an "OnClick()" event that will increment the variable each time.

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 03 August 2006 - 06:34 PM

buttons dont submit forms. You'll want to use submit button and not a normal button.

#6 baho

baho
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 03 August 2006 - 06:42 PM

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.

#7 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 03 August 2006 - 07:39 PM

<script language="javascript">
<!---
static=0;
function Increment() {
static++;
}
//-->
</script> 

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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users