-1

当我单击一个链接时,我正在从数据库中写入我的 $_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);
});
4

1 回答 1

0

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

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

编辑:像这样:

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....
}
于 2012-09-25T15:30:32.843 回答