Shifty
Shifty

Reputation: 112

How can I use session in script?

I can write out the images src with this script:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("img.film").click(function () {
            alert($(this).attr('src'));
        });
    });
</script>

But how can I fill it to a session, which I can use in php? I know that this row has to be changed, but for what?

alert($(this).attr('src'));

Thanks.

Upvotes: 2

Views: 364

Answers (2)

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35973

you need to make for example an ajax call to set your php session like this:

$(document).ready(function () {
        $("img.film").click(function () {
            var src = $(this).attr('src')
            $.ajax({
                 type: 'POST',
                 url: "set_session.php",
                 data:{your_var:src},
                 success: function(resultData) { 
                       alert("Save Complete") }
                 });
        });
    });

and in the same directory you need to create a file called set_session.php

session_start();
$_SESSION['your_key'] = $_POST['your_var'];

It's important that session_start() is in the first line of both file

Upvotes: 1

James M. Lay
James M. Lay

Reputation: 2470

Well, if you want it to be read by PHP, you can just use a cookie.

$(document).ready(function () {
    $("img.film").click(function () {
        document.cookie = "image_src=" + $(this).attr('src');
    });
});

Then, on PHP, just get the cookie from the cookie variable:

$_COOKIE['image_src'];

Upvotes: 1

Related Questions