fatmart Posted February 26, 2008 Share Posted February 26, 2008 Hi. I want to do a button that when you click on it, the number in the input value increases. <form method="post" name="form" action="<?php echo $PHP_SELF; ?>"> <input type="text" value="1" name="" /> <input type="submit" value="+" name="submit" /> </form> I know I have to do a loop but I don't know how to use it for what I want to do.. Like everytime I post, the value in the input increases by 1. Thanks in advance Link to comment https://forums.phpfreaks.com/topic/93122-loop-increment-on-button-click/ Share on other sites More sharing options...
blackwinter Posted February 26, 2008 Share Posted February 26, 2008 So what your saying is you just want the value of the test to go up one ? Link to comment https://forums.phpfreaks.com/topic/93122-loop-increment-on-button-click/#findComment-477081 Share on other sites More sharing options...
rhodesa Posted February 26, 2008 Share Posted February 26, 2008 This is a JavaScript topic, not a PHP topic, but nonetheless: <script type="text/javascript"> function increaseValue ( eleId ) { var ele = document.getElementById(eleId); ele.value = parseInt(ele.value) + 1; } </script> <input type="text" value="1" id="incrementer" /> <input type="button" value="+" onclick="increaseValue('incrementer');" /> Link to comment https://forums.phpfreaks.com/topic/93122-loop-increment-on-button-click/#findComment-477085 Share on other sites More sharing options...
fatmart Posted February 26, 2008 Author Share Posted February 26, 2008 I thought there was a way to do this in php.. I knew about the javascript code but I'm trying to minimalize it as much as I can..anyway thank you! Link to comment https://forums.phpfreaks.com/topic/93122-loop-increment-on-button-click/#findComment-477092 Share on other sites More sharing options...
rhodesa Posted February 26, 2008 Share Posted February 26, 2008 You could do it in PHP, but the page would have to submit every time. JavaScript is a MUCH better approach in my opinion. Link to comment https://forums.phpfreaks.com/topic/93122-loop-increment-on-button-click/#findComment-477095 Share on other sites More sharing options...
haku Posted February 26, 2008 Share Posted February 26, 2008 If you are going to use javascript, you should use both for those people that have javascript turned off. counter.php: <?php if(!isset ($_GET['count'])) { $j = 1; } else { $j = $_GET['count']; } ?> <form action="counter.php?count=<?php echo ($j+1); ?>" method="get"> <input type="text" value="<?php echo $j; ?>" /> <input type="submit" value="+" name="submit" /> </form> I just put it together now without testing, but I think it should work. edit: But it has to reload everytime. There is no way around that. Link to comment https://forums.phpfreaks.com/topic/93122-loop-increment-on-button-click/#findComment-477114 Share on other sites More sharing options...
fatmart Posted February 26, 2008 Author Share Posted February 26, 2008 haku, thanks for your code but it's not working.. :/ Link to comment https://forums.phpfreaks.com/topic/93122-loop-increment-on-button-click/#findComment-477119 Share on other sites More sharing options...
fatmart Posted February 26, 2008 Author Share Posted February 26, 2008 Oh sorry, it's working now, I just had to replace the ''get'' by ''post'' thanks a lot! Link to comment https://forums.phpfreaks.com/topic/93122-loop-increment-on-button-click/#findComment-477121 Share on other sites More sharing options...
haku Posted February 26, 2008 Share Posted February 26, 2008 No worries. It should have worked with get though! Link to comment https://forums.phpfreaks.com/topic/93122-loop-increment-on-button-click/#findComment-477125 Share on other sites More sharing options...
fatmart Posted February 26, 2008 Author Share Posted February 26, 2008 Yeah, it's weird .. now I want it to stop when it reaches 20.. lol Should I use a break; ? Link to comment https://forums.phpfreaks.com/topic/93122-loop-increment-on-button-click/#findComment-477130 Share on other sites More sharing options...
haku Posted February 26, 2008 Share Posted February 26, 2008 <?php if(!isset ($_GET['count'])) { $j = 1; } else { $j = $_GET['count']; } ?> <form action="counter.php?count=<?php echo ($j+1); ?>" method="get"> <input type="text" value="<?php echo $j; ?>" /> <input type="submit" value="+" name="submit"<?php if($j==20){ echo " disabled=\"disabled\""; ?> /> </form> edit: changed it a little Link to comment https://forums.phpfreaks.com/topic/93122-loop-increment-on-button-click/#findComment-477171 Share on other sites More sharing options...
fatmart Posted February 26, 2008 Author Share Posted February 26, 2008 wow cool.. now I'm trying to figure out how to decrement.. if you have time........ i'm working on it but I can't find Link to comment https://forums.phpfreaks.com/topic/93122-loop-increment-on-button-click/#findComment-477212 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.