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

Link to comment
Share on other sites

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
Share on other sites

<?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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.