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'];
  }

  ?><scripttype='text/javascript'>functionsubmitForm(x) {
     if(x == 'prev') {
     document.getElementById('count').value--;
     } else {
     document.getElementById('count').value++;
     }

     document.forms["form"].submit();
   }
   </script><formaction='hidfield.php'method='get'name='form'><inputtype='hidden'name='count'id='count'value='<?phpecho$count; ?>'></form><inputtype='submit'name='prev'value='prev'onclick="submitForm('prev')"><inputtype='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?"