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

后端 未结 8 1714
情歌与酒
情歌与酒 2020-12-01 00:00

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 variab

相关标签:
8条回答
  • 2020-12-01 00:39

    A lot of responses on here are addressing the how but not the why. PHP $_SESSION key/value pairs are stored on the server. This differs from a cookie, which is stored on the browser. This is why you are able to access values in a cookie from both PHP and JavaScript. To make matters worse, AJAX requests from the browser do not include any of the cookies you have set for the website. So, you will have to make JavaScript pull the Session ID cookie and include it in every AJAX request for the server to be able to make heads or tails of it. On the bright side, PHP Sessions are designed to fail-over to a HTTP GET or POST variable if cookies are not sent along with the HTTP headers. I would look into some of the principles of RESTful web applications and use of of the design patterns that are common with those kinds of applications instead of trying to mangle with the session handler.

    0 讨论(0)
  • 2020-12-01 00:43

    It works on firefox, if you change onClick() to click() in javascript part.

    $("img.foo").click(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});
    });

    0 讨论(0)
  • 2020-12-01 00:45

    Similar to Luke's answer, but with GET.

    Place this php-code in a php-page, ex. getpage.php:

    <?php
    $_SESSION['size'] = $_GET['size'];
    ?>
    

    Then call it with a jQuery script like this:

    $.get( "/getpage.php?size=1");
    

    Agree with Federico. In some cases you may run into problems using POST, although not sure if it can be browser related.

    0 讨论(0)
  • 2020-12-01 00:51

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

    0 讨论(0)
  • 2020-12-01 00:56

    I also designed a "php session value setter" solution by myself (similar to Luke Dennis' solution. No big deal here), but after setting my session value, my needs were "jumping onto another .php file". Ok, I did it, inside my jquery code... But something didn't quite work...

    My problem was kind of easy:

    -After you "$.post" your values onto the small .php file, you should wait for some "success/failure" return value, and ONLY AFTER READING THIS SUCCESS VALUE, perform the jump. If you just immediately jump onto the next big .php file, your session value might have not become set onto the php sessions runtime engine, and will you probably read "empty" when doing $_SESSION["my_var"]; from the destination .php file.

    In my case, to correct that situation, I changed my jQuery $.post code this way:

    $.post('set_session_value.php', { key: 'keyname', value: 'myvalue'}, function(ret){
        if(ret==0){
            window.alert("success!");
            location.replace("next_page.php");
        }
        else{
            window.alert("error!");
        }
    });
    

    Of course, your "set_session_value.php" file, should return 'echo "0"; ' or 'echo "1"; ' (or whatever success values you might need).

    Greetings.

    0 讨论(0)
  • 2020-12-01 01:02

    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'];
    ?>
    
    0 讨论(0)
提交回复
热议问题