Stanislas Piotrowski
Stanislas Piotrowski

Reputation: 2694

trouble with jquery and session var

I need to change some session var while clicking on image.

So I added a class on the concerned pictures (that pictures are flags). It is for translating a website.

In my website, language is defined using session var, and by default it is set to english.

So poeple can change them

Here is a kind of image <img src="images/fr.png" title="French" alt="French" class="flag" id="fr">I get. So I added class and Id,

then I've written the following code using jquery to make it on the same page with no reload :

<script type="text/javascript">
            $(document).ready(function () {
                $("img.flag").click(function(){
                    // Get the src of the image
                    var src = $(this).attr("id");

                    // Send Ajax request to backend.php, with src set as "img" in the POST data
                    $.post("lib_php/session.php", {"lang": src});
                })
            })
        </script>

On the page where I send the post I have simply that code :

<?php
session_start();
    // do any authentication first, then add POST variable to session
    $_SESSION['lang'] = $_POST['lang'];
?>

The trouble is that nothing is changing, all remains the same.

I do not know where I'm wrong

Anylind of help will be much appreciated.

Upvotes: 0

Views: 227

Answers (3)

Bora
Bora

Reputation: 10717

You have to use session_start(); in file that you are using <script>

<?php session_start(); ?>

<script type="text/javascript">
            $(document).ready(function () {
                $("img.flag").click(function(){
                    // Get the src of the image
                    var src = $(this).attr("id");

                    // Send Ajax request to backend.php, with src set as "img" in the POST data
                    $.post("lib_php/session.php", {"lang": src});
                })
            })
        </script>

Alternative Function

I tried with $.ajax and it works. Please try this one

<img src="images/fr.png" title="French" alt="French" class="flag" id="fr" />

<script type="text/javascript">
$('img.flag').click(function() {
    var src = $(this).attr("id");
    $.ajax({
        type:'POST',
        data: {lang: src},
        url:'lib_php/session.php'
    });
});
</script>

Note: Important that <script type="text/javascript"> is after the img tag

Upvotes: 1

scraaappy
scraaappy

Reputation: 2886

try the following to check if everything is ok :

$.post("lib_php/session.php", {"lang": src} ,function(data)
{
    alert (data);
});

then in session php :

echo  $_SESSION['lang'];

Upvotes: 1

Ian Clark
Ian Clark

Reputation: 9347

It's difficult to say by just looking at the code above. As far as I'm aware the syntax looks fine for both the jQuery and the PHP. I'd imagine your issue would be caused by one of the following:

  • You're not POSTing the information to the correct page. Way to check: in your PHP echo $_POST['lang']
  • You're not including session_start() across all of your PHP pages.
  • You're not even doing the post due to not including jQuery properly - check your browsers console to see if a POST call is actually being fired.

Upvotes: 2

Related Questions