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 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab