Riyaz
Riyaz

Reputation: 139

SESSION returns old value when echoed in php

<script language="javascript">
            function emergency()
            {
                if(document.getElementById('emer').checked == true)
                {
                    <?php $_SESSION['total2']= $_SESSION['total1'] + 50; ?>
                    document.getElementById('emr').innerHTML = "<b>Payable Amount: </b>" + <?php echo  $_SESSION['total2']; ?>;
                }
                if(document.getElementById('emer').checked == false)
                {
                    <?php $_SESSION['total2']= $_SESSION['total2'] - 50; ?>
                    document.getElementById('emr').innerHTML = "<b>Payable Amount: </b>" + <?php echo  $_SESSION['total2']; ?>;
                }
            }
            </script>

here I am adding $_SESSION['total1']+50 and put the value in new session SESSION['total2']. The problem is that , when I echo this new $_SESSION['total2'] out side script,ie, in the page like

<?php echo $_SESSION['total2'];?>

it returns the value of $_SESSION['total1']).

Upvotes: 1

Views: 1292

Answers (2)

Nishant Solanki
Nishant Solanki

Reputation: 2128

Are you calling a javascript function to change value of php session variable?.. this could not be possible because, php is a server side language and javascript is client side so php code gets executed when the page is loaded..

so if you want to do this on javascript function call,, than you need to use ajax...

Upvotes: 4

Janaka Dombawela
Janaka Dombawela

Reputation: 1357

to get the new value of $_SESSION['total2'] you have to run the code on server side again. that means before getting the new value for total2 session variable you have to reload the appropriate php file.

Upvotes: 1

Related Questions