baho Posted August 3, 2006 Share Posted August 3, 2006 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!!! Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted August 3, 2006 Share Posted August 3, 2006 You cant without submitting the form. You cannot use PHP like you can with javascript. However you can if you use AJAX. Quote Link to comment Share on other sites More sharing options...
CTM Posted August 3, 2006 Share Posted August 3, 2006 He could use Javascript alone too, depending on his purpose. Quote Link to comment Share on other sites More sharing options...
HeyRay2 Posted August 3, 2006 Share Posted August 3, 2006 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 $staticif(isset($_POST['static'])){ $static = $_POST['static'];} else { $static = 0;}// Check if the form was submittedif($_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. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted August 3, 2006 Share Posted August 3, 2006 buttons dont submit forms. You'll want to use submit button and not a normal button. Quote Link to comment Share on other sites More sharing options...
baho Posted August 3, 2006 Author Share Posted August 3, 2006 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. Quote Link to comment Share on other sites More sharing options...
HeyRay2 Posted August 3, 2006 Share Posted August 3, 2006 [code]<script language="javascript"><!---static=0;function Increment() {static++;}//--></script> <form><input type=button name=button value="CLICK" onClick="Increment()"></form> [/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.