Skip to content Skip to sidebar Skip to footer

How Can I Increase A Number By Activating A Button To Be Able To Access A Function Each Time The Button Is Increased?

I am trying to create a button, that whenever it is clicked (Onclick) it changes a value (number) so t

Solution 1:

You could use an object, in which you set the two variables that you need to update on click: var obj = { nextClicked : 0, prevClicked : 0 };

function buttonClick(type) {
    if(type == "prev") {
         obj.nextClicked++;
    }
    if(type == "next") {
         obj.prevClicked++
    }
}
<button type="button" onclick="buttonClick('next')">Next</button>
<button type="button" onclick="buttonClick('prev')">Prev</button>

Since you are using ajax, the variables would not reset, unless you refresh the page


Solution 2:

You could use a php session to store the "page" number you're currently on and then increase or decrease based upon which button is clicked (you could use ajax or a simple form to send the event data).


Solution 3:

use a hidden field to hold the value, and an onclick function to increase it and submit the form.

 <?

 if(!isset($_GET['count'])) {
  $count = 0;
  } else {
  $count = $_GET['count'];
  }

  ?>

   <script type='text/javascript'>
   function submitForm(x) {
     if(x == 'prev') {
     document.getElementById('count').value--;
     } else {
     document.getElementById('count').value++;
     }

     document.forms["form"].submit();
   }
   </script>

   <form action='hidfield.php' method='get' name='form'>
   <input type='hidden' name='count' id='count' value='<?php echo $count; ?>'>
   </form>

   <input type='submit' name='prev' value='prev' onclick="submitForm('prev')">
   <input type='submit' name='next' value='next' onclick="submitForm('next')">

Solution 4:

Add this to your webpage and refresh a few times.

<?php
session_start();
echo $_SESSION['count']++;

Can be tested here:

http://codepad.viper-7.com/qXdj8M


Post a Comment for "How Can I Increase A Number By Activating A Button To Be Able To Access A Function Each Time The Button Is Increased?"