1

I'm trying to assign value of JavaScript variable to php session. Please see my code below -

<script type="text/javascript">
<?php $_SESSION['historyClass'] = "";?>

var myClass = $(this).attr("class");
if(myClass == 'trigger'){
  <?php $_SESSION['historyClass'] = "trigger";   ?>
}
else{
  <?php $_SESSION['historyClass'] = "trigger active"; ?>
}

alert('<?php echo $_SESSION['historyClass']; ?>')

</script>

In myClass variable, i'm getting 2 values 1) trigger 2) trigger active

Whatever the value I'll get, I want to store it in php session. But when I alert the session value it is always giving me "trigger active". It means it is always going to else part. I have checked the 'if' condition by alerting in it, the control is going properly in "If" and "else" part.

What is the problem? Am I doing something wrong?

Thanks in advance.

1

4 Answers 4

3

PHP is processed first, and then javascript is executed, so it's impossible to directly assign values to php variables.

instead you could send http requests from javascript (Ajax) to php scripts to save your data.

Sign up to request clarification or add additional context in comments.

1 Comment

@All : Okay.. got the point. Thanks for quick reply... actually I don't want to save the data. Whatever the value I'll get in session, I want to apply that class to a particular div while refreshing or populating the page. So i have only one solution to store myClass value in session.
0

This won't work. PHP is executed on the server, and JS on the client. That means that the php is running before the JS is parsed. I suggest you look at Ajax if you want to run PHP from javascript (specifically the jQuery library and its ".get()" function).

What's happening is that the PHP is parsed, and doesn't see any JS, so it runs as normal, and sets the session to trigger, and then trigger active. Then javascript comes along on the client, and the client doesn't see any PHP code,so doesn't run it.

Comments

0

this wont work. the php code runs on serverside, js - on client. so php is executed before the page is shown to the user. it means that first is executed $_SESSION['historyClass'] = "trigger"; than $_SESSION['historyClass'] = "trigger active"; and obviously the value will be trigger active

Comments

0

yes you are doing something wrong.

javascript runs on client side and php on server side. so your code runs first on server side and then on the client. thats why you can't do it like you did. a common way to transfer javascript data to a php script is, writing the value to a hidden field, so that it gets submitted to the server.

just create a hidden field and fill it with a value from javascript

<script type="text/javascript">
    function valToPHP(name,value){
        document.getElementById(name).value = value;
    }
</script>
...
</head>
<body>
<input type="hidden" id="myField" name="myField" value="" />
...

you can then read it in your php script like that

$_GET["myField"] or $_POST["myField"]

this depends on your method of the form

1 Comment

Thanks for solutions. But every time I'm not submitting the form. I have many links on my page so for every link it won't possible to send the hidden value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.