2

I want to insert javascript value into the php session variable but inside the javascript function.

Here is what I have tried and it is not working:

function languageChange()
{
 var lang = $('#websites1 option:selected').val();
 <?php $_SESSION['SESS_LANGUAGE']?> = lang;
 alert(<?php echo $_SESSION['SESS_LANGUAGE']?>);
}
user123_456
  • 5,635
  • 26
  • 84
  • 140
  • 2
    PHP session variables are set in php. What you have will never work, it's not even logical. You could try send a request to a server side php script that will set variable session. – Angel Jul 18 '12 at 11:26
  • If you ever want to, however, you can recall session variables into Javascript like this: `var something = ;` – Alfo Jul 18 '12 at 11:31

2 Answers2

9

You cannot access it directly (the way you are doing). However, it can be done using AJAX. Here is the perfectly working solution.

Here is the HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>1</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
    <label for="websites1">Websites</label>
    <select name="websites1" id="websites1">
        <option value="Design">Design</option>
        <option value="Dev">Dev</option>
        <option value="Ecom">Ecom</option>
    </select>
</form>
<script type="text/javascript" src="js/jquery_1.7.1_min.js"></script> 
<script type="text/javascript">

$(document).ready(function() {

    function languageChange()
    {
         var lang = $('#websites1 option:selected').val();

        return lang;
    }


    $('#websites1').change(function(e) {                    

        var lang = languageChange();

        var dataString = 'lang=' + lang;

        $.ajax({

            type: "POST",
            url: "pass_value.php",
            data: dataString,
            dataType: 'json',
            cache: false,
            success: function(response) {

                    alert(response.message);

                }
        });

        return false;

    });

});

</script>
</body>
</html>

Here is the AJAX part:

//pass_value.php

<?php session_start();

$_SESSION['SESS_LANGUAGE'] = $_POST['lang'];

$_SESSION['SESS_LANGUAGE'] = 'This is my PHP session var --->'.$_SESSION['SESS_LANGUAGE'];

print json_encode(array('message' => $_SESSION['SESS_LANGUAGE']));
die();


?>

EDIT 1:

Once you run the code, simply select any of the other options from the dropdown menu and you will receive an alert that gives you the value of the php session variable along with the custom text that I added to it.

EDIT 2:

If you want to run my solution on your end, make sure you point to core jQuery js file correctly. My jquery code points to src="js/jquery_1.7.1_min.js". Make sure you update this.

Devner
  • 6,825
  • 11
  • 63
  • 104
  • Shouldn't be `$_SESSION['SESS_LANGUAGE'] = $_POST['dataString'];` ?p.s. I'm not JSON practice, indeed – A_nto2 Jul 18 '12 at 13:02
  • Nope, dataString is the name value pair. It's in the format of name=value. Normally, the serialized values of a form are sent this way. I think because there is only 1 field, you may have got confused. You could use a form and then use `serialize()` on it to send the values of all fields in the form using AJAX. Doing what you suggested will break the functionality. – Devner Jul 18 '12 at 13:16
  • This is working and I am printing it in the php but I am not getting the value in the javascript. Any idea? – user123_456 Jul 18 '12 at 17:18
  • @Devner Hi I have just tried your code and I am not getting anything with alert function. I have tried to put breakpoint on that line but I am not getting inside. Are you sure you are entering that success state? – user123_456 Jul 18 '12 at 18:06
  • What do you mean printing in PHP but not in javascript? See the statement `alert(response.message);` ? This would be your javascript alert. I didn't use any `echo $_SESSION['SESS_LANGUAGE'];`. So how are you printing it in PHP? You have most likely skipped reading my 2 Edits above? Did you read them? Are you pointing to the correct location of the jquery file? Are you using the code as I have given it to you or have you included it within your webpage/code & experimenting with it? If later, then you must be doing something wrong. It works perfectly fine on my end. – Devner Jul 18 '12 at 18:11
0

Javascript does not have access to PHP-variables. The client (web browser/javascript) only has access to the Cookie or Cookieless ID (Querystring) associated with the session.

Sessions are used this way for the purpose of not letting the client modify settings associated with the session without going through the server. The less "secure" way is to use cookies for all settings. Something like language-setting might be a good idea to store in a cookie rather than the session.

In order to solve this, you could make a page which takes a session property name (like your "SESS_LANGUAGE") and a value which puts it in the session using php. I strongly advise against this because any user could then set any session variable.

The best way I can imagine is that you send an AJAX-call to a page saying that you want to change the language.

The called URL would be something like this: changelanguage.php?lang=en_US

And the php-code for changelaguange.php: $_SESSION['SESS_LANGUAGE'] = $_GET['lang'];

Manikandan C
  • 668
  • 1
  • 9
  • 22
DavidMB
  • 41
  • 2