Jump to content

Loop increment on button click


fatmart

Recommended Posts

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

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');" />

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.

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

Archived

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

×
×
  • Create New...

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.