-1

I want to set session inside the javascript. But all the time this function return false value. Please tell me the error of this code.

<script>
function myFunction(){

var r=confirm("Would you like to add another item ?");
if (r==true) {
    alert('ok');
    var varname = '<?php echo $_SESSION["redirect"]=1; ?>';     
 } 

 if(r==false)   {
     alert('bad');
    var varname = '<?php echo $_SESSION["redirect"]=2; ?>';
 }  
}
</script>
vusan
  • 5,221
  • 4
  • 46
  • 81
Nytram
  • 521
  • 1
  • 6
  • 21

2 Answers2

1

You can't evaluate PHP on the client. Do an AJAX request to the server setting the session variable instead.

0

Javascript runs on the client side, so you'll need to perform an AJAX call to a server side script that will change the sessions value.

Something in the lines of:

function changeSession()
{
    $.ajax({
        url: /change_session.php?value=1&key=redirect,
        dataType:"html"
    });
}

And in change_session.php:

<?php $_SESSION[$_REQUEST['key']]=$_REQUEST['value'] ?>
Noam
  • 3,341
  • 4
  • 35
  • 64