douli7841 2012-09-14 19:20
浏览 39
已采纳

单击链接时设置会话变量

I want to set a session variable to 0 when any of a certain set of links are clicked. To do this I have put the following in my javascript file:

$(window).load(function () {
    $("#hdr li a").click(function () {
        $.ajax({
            type: "POST",
            url: "clear.php",
            data: "width=0"
        });
    });
});

(Ignore data: "width=0"... I don't use this data in clear.php. I put it there because I thought maybe I had to specify a data field.)

and in the file 'clear.php' I simply have:

<?php session_start();

$_SESSION['name'] = 0;

?>

So, the idea is that when any of the links in #hdr li are clicked, the user should be taken to the page that the link points to, via clear.php, which sets the session variable to 0.

This works in some browsers (Firefox and Chrome) but not in others (e.g., Safari).

Is this the standard/correct way to implement what I want? Also, how does the browser know where to go after visiting clear.php? Somehow it works, but my first thought was that I should pass the final destination URL into clear.php, and then use "header" to move from clear.php to the final destination.

  • 写回答

4条回答 默认 最新

  • dongmou3615 2012-09-14 19:30
    关注

    Is Ajax required? If your re-directing the user to another page & you simply want to pass some data to that page then it may be simpler to include that data in your URL.

    <a href="http://yoursite.com/interesting_page.php?new_session_variable=whatever">Link</a>
    

    Now your php would be simple:

    $_SESSION['name'] = $_GET['new_session_variable'];
    

    Now you've removed your dependency on JavaScript, does that make sense? :)


    I feel it might be worth mentioning that your approach would be appropriate in certain situations, for example: if you wanted the user to be able to mark one of the links on the page as a favourite. Rather than redirecting them to the same page and reloading the majority of the pages content you might:

    <a class="favourite" data-linkid="link123" href="mylink.php">My Link</a>
    

    // Ensure your page has finished loading aka: 'ready' (almost always)
    $(document).ready(function() {
    
        // Listen for the click event
        $('.favourite').on('click', favoriteLink);
    
        // On the click - post the update via ajax and update your interface
        function favoriteLink(event) {
            event.preventDefault();
    
            // Lets get the link id from our data attribute
            var favourite_link = $(this).data('linkid');
    
            // Post that information via ajax
            $.post('ajax_handler.html', { link : favourite_link }, function(data) {
    
                // And finally do something with the result!
                $('.result').html(data);
            });
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?