Setting a PHP $_SESSION['var'] using jQuery Setting a PHP $_SESSION['var'] using jQuery php php

Setting a PHP $_SESSION['var'] using jQuery


You can't do it through jQuery alone; you'll need a combination of Ajax (which you can do with jQuery) and a PHP back-end. A very simple version might look like this:

HTML:

<img class="foo" src="img.jpg" /><img class="foo" src="img2.jpg" /><img class="foo" src="img3.jpg" />

Javascript:

$("img.foo").onclick(function(){    // Get the src of the image    var src = $(this).attr("src");    // Send Ajax request to backend.php, with src set as "img" in the POST data    $.post("/backend.php", {"img": src});});

PHP (backend.php):

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


Might want to try putting the PHP function on another PHP page, and use an AJAX call to set the variable.


Whats you are looking for is jQuery Ajax. And then just setup a php page to process the request.