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 Quote Link to comment 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 ? Quote Link to comment 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');" /> Quote Link to comment 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! Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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.. :/ Quote Link to comment 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! Quote Link to comment 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! Quote Link to comment 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; ? Quote Link to comment 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 Quote Link to comment 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 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.