1
<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']).

Balaji Kandasamy
  • 4,446
  • 10
  • 40
  • 58
Riyaz
  • 139
  • 11

2 Answers2

4

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

Nishant Solanki
  • 2,119
  • 3
  • 19
  • 32
  • :now i just changed the code like $tot=$_SESSION['TOTAL1']+50 and outside javascript I initialized a session normally as $_SESSION['total2']=$tot .Then I echoed it. Still the same result. – Riyaz Mar 06 '14 at 11:27
  • the thing is that **you can not change php variable value using javascript**.. but by using `ajax` you can.. please refer this answer.. http://stackoverflow.com/questions/11540000/how-to-insert-javascript-value-into-the-php-session-variable – Nishant Solanki Mar 06 '14 at 11:41
1

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.

Janaka Dombawela
  • 1,337
  • 4
  • 26
  • 49
  • :innerhtml displays correct values.but when i echo value anywhere else ,it returns session['total1'] value even after reloading the page. – Riyaz Mar 06 '14 at 11:08