Ace Caserya
Ace Caserya

Reputation: 2935

How to change session variable from php function using button onclick?

All front-end programming only please.

Here is the actual site I am currently working on: http://alvincaseria.me/SJA100/ I have buttons which displays like this way:

What I'm trying to achieve is a php page that has a session variables that saves the year (example: 1991). Then as certain buttons are click, the variable changes it's value.

Example:

  1. When I click on the button [after 1991], the session runs a function that is suppose to increment the session variable named "batch"
  2. When I click on the button [before 1991], the session runs a function that is suppose to decrement the same variable.

Currently I am running a "Onclick=location.href..." but i want something like calling functions() as mentioned in this reference. How to Call a PHP Function on the Click of a Button

I am pretty new to php and ajax and I do not know how to call the mentioned reference to fit my current code since it is outside my page. If possible, can I do it without ajax?

Here is my Current Code, you can also check my current page as mentioned above.

  <?php
  session_start();
  // store session data
  $_SESSION['batch']=1991;
  ?>

  <html lang="en">
    <head> ... </head>
    <body>
      <?php 
          if($_GET['button1']){add1();}
          if($_GET['button2']){min1();}

          function add1()
          {
            $_SESSION['batch']=$_SESSION['batch']+1; 
          }

          function min1()
          {
            $_SESSION['batch']=$_SESSION['batch']-1;  
          }
      ?>

      ... more codes

      // After Year Chosen
      <div class="child"><button type="button" class="btn-styleBT">Short Stories of (Year chosen plus 1)</button></div> 
      <div class="child"><button type="button" class="btn-styleCT" id="btn1" name="btn1" onClick='location.href="?button1=1"'>
            <?php
            //retrieve session data
            echo "after ".$_SESSION['batch'];
            ?>
      </button></div>          
      <div class="child"><button type="button" class="btn-styleBT">Batch of (Year chosen plus 1)</button></div>

      ... more codes Middle divs

      <div class="child"><button type="button" class="btn-styleBT">Short Stories of (Year chosen minus 1)</button></div>
      <div class="child"><button type="button" class="btn-styleCT" id="btn2" name="btn2" onClick='location.href="?button2=1"'>
            <?php
            //retrieve session data
            echo "before ".$_SESSION['batch'];
            ?>
       </button></div>
       <div class="child"><button type="button" class="btn-styleBT">Batch of (Year chosen minus 1) </button></div>

      ... more codes 

Any ideas or suggestions?

Upvotes: 0

Views: 2975

Answers (1)

scrowler
scrowler

Reputation: 24405

The idea of sessions is that they persist across page requests - you should only set the variable on line 4 if it's not set already:

if(!isset($_SESSION['batch']))
    $_SESSION['batch'] = 1991;

Upvotes: 1

Related Questions