你不能通过jQuery单独做;你需要一个Ajax(你可以用jQuery做)和一个
PHP后端的组合。一个非常简单的版本可能如下所示:
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'];
- ?>