如果$ _SESSION变量已更改,如何刷新站点

时间:2012-09-25 15:25:15

标签: php mysql session refresh

当我点击一个链接时,我正在用数据库写我的$ _SESSION变量。

public function getProjectById($id){
  $query="SELECT * FROM projects WHERE id=\"$id\"";
  $result=mysql_query($query);
  $num=mysql_numrows($result);

  while ($row = mysql_fetch_object($result)) {
    $_SESSION['projectid'] = $row->id;
    $_SESSION['projecttitle'] = $row->title;
    $_SESSION['projectinfo'] = $row->info;
    $_SESSION['projecttext'] = $row->text;
    $_SESSION['projectcategory'] = $row->category;
  }
}

现在我的变量被覆盖了,我想在index.php中显示这些变量,如下所示:

<div id="textContent">
   <?php
    if(isset($_SESSION['projecttext']) && !empty($_SESSION['projecttext'])) {
        echo $_SESSION['projecttext'];
    }else {
        echo 'No text';
    }
?></div>

但当然,我的页面不会自动刷新。我怎么能这样做?

编辑:调用代码:

$(document).on("click", ".sublink", function(){
    var subsite = $(this).data('subsite');
    var category = $(this).data('category');
    var title = $(this).data('title');
    var info = category + "/" + title;
    var lower = info.toLowerCase();
    var nospaces = lower.split(' ').join('');
    $('#imageContent').load('php/getImages.php?info='+nospaces);
    $('#textContent').load('php/getProjectById.php?id='+subsite);
});

1 个答案:

答案 0 :(得分:0)

有几种方法可以实现这一点(javascript替换用户已经可见的html代码,或用于刷新整个页面),但我想你的函数调用是由onclick触发的(调用特定的php-page )你可能会用Meta Refresh

做得最好

重新加载整个页面。 请注意,现在这被认为是不合时宜的; - )

编辑:像这样:

public function getProjectById($id){
  $query="SELECT * FROM projects WHERE id=\"$id\"";
  $result=mysql_query($query);
  $num=mysql_numrows($result);

  while ($row = mysql_fetch_object($result)) {
    $_SESSION['projectid'] = $row->id;
    $_SESSION['projecttitle'] = $row->title;
    $_SESSION['projectinfo'] = $row->info;
    $_SESSION['projecttext'] = $row->text;
    $_SESSION['projectcategory'] = $row->category;
  }
// everything done here, do a meta refresh here
?> <meta http-equiv="refresh" content="0;URL='http://example.com/'"> <?php
// worse alternative using .. echo("<meta....
}