Viewed   108 times

I need to set a PHP $_SESSION variable using the jQuery. IF the user clicks on an image I want to save a piece of information associated with that image as a session variable in php.

I think I can do this by calling a php page or function and appending that piece of info to the query string.

Any ideas. I have found little help through google.

thanks mike



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:


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


    // 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):

    // do any authentication first, then add POST variable to session
    $_SESSION['imgsrc'] = $_POST['img'];
Saturday, August 6, 2022

Here's what I would do (cut some code out for quickness):

HTML (you don't require javascript void code if using jQuery):

<ul id="set-display">
    <li><a href="" id="list">Listview</a></li>
    <li><a href="" id="grid">Gridview</a></li>


$('#set-display a').click(function(e) {
    var type = $(this).attr('id');
    var view = (type === 'grid') ? 'gridview' : 'listview';
    $.post('library/fx.behaviour.php', { display: view });

    // you can use the view or type variable to trigger your other code


session_start(); // if not called already

// if POST display not empty and is expected string then use it, otherwise default to 'listview'
$_SESSION['display'] = ( ! empty($_POST['display']) && in_array($_POST['display'], array('listview', 'gridview'))) ? $_POST['display'] : 'listview';

I used $_POST['display'] rather than setdisplay for naming consistency. It might also be worth checking your jQuery call gets a valid 200 response. In Chrome you can do this by going to the Inspector, selecting the network tab, click the button, and watch for the request.

Thursday, October 6, 2022

Specify the data and dataType options:

var jsonstr = JSON.stringify(result);

          url: "ajax.php",
          data:'json=' + jsonstr,
          success: function(){
              alert('Test results submitted!');

From php, you can get json string using:

Monday, October 3, 2022

My suggestion from your previous question still stands: please compare session ids.

The solution might be as simple as your browser not accepting session cookies.

You retrieve the session id by calling session_id(). Do that right after session_start() it should give you a constant value if the session is the same. Otherwise for every request a new session is instantiated.

Also check C:wamptmp. A gazillion files in this directory might indicate fresh sessions for each request.

EDIT Since we've confirmed new sessions per request, it's time to find out whether session cookies are accepted. Check the settings of your browser and confirm that a cookie for your domain (I guess it's "localhost") with the name PHPSESSID can be found.

Thursday, September 22, 2022

Simply put: You can't, directly.

PHP is a server-side language while javascript is a client-side one, which means that there's no other connection between them other than the document you receive and interacts with you. The sessions are stored and managed by the server.

You can, however, as War10ck suggests, call to a server-side script that clears the session. Something like this:


     * session_start();
     * Starting a new session before clearing it
     * assures you all $_SESSION vars are cleared 
     * correctly, but it's not strictly necessary.
    header('Location: continue.php'); 
    /* Or whatever document you want to show afterwards */


<script type="text/javascript">
    function logout() {
        document.location = 'logout.php';
    LogoutButton.addEventListener('click', logout, false);

<button id="LogoutButton">Log out</button>

Or even performing an asynchronous call:

function logout() {
    var xhr = new XMLHttpRequest();
    xhr.onload = function() {
        document.location = 'continue.php';
    }'GET', 'logout.php', true);
Wednesday, August 31, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :