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

后端 未结 8 1851
情歌与酒
情歌与酒 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 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:

    
    
    
    

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

    
    

提交回复
热议问题