jquery Fancybox重新加载模态内容

时间:2011-07-24 20:35:18

标签: php jquery ajax fancybox modal-dialog

我正在使用Fancybox在我的网站上显示模态。此模式一次从新闻帖的评论中加载10条评论。我对jquery和ajax的熟练程度要低一些,并希望得到一些帮助,因为我不能按照自己的意愿去工作。

显示信息没有问题,但是我遇到的问题是我似乎无法显示任何内容,只有第一页评论。

我用来触发模态的代码:

<script type="text/javascript"> 
    $(document).ready(function() {

        $("a.comments").fancybox();
        });

使用以下链接触发模态:

<a class="comments" href="./process/news.php?n='.$news['id'].'&page=1">View Comments/Add Comment ('.$comments_total.')</a>

模态显示以下代码:

    <?php session_start();
//no caching, we're using ajax
header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("cache-control: no-store, no-cache, must-revalidate");
header("Pragma: no-cache");

//base
include ("../includes/config.php");
include ("functions.php");
checkLogin();

//variables
$act = prepare($_GET['act']);
$n = prepare($_GET['n']);
$page = prepare($_GET['page']);
$modal = $_POST['nyroModal'];

//Possible pages
switch($act)
{
  default:
  {
    if(!empty($n))
    {
       $css = '<link rel="stylesheet" href="stylesheets/Main.css">';
      $query = mysql_query("SELECT * FROM news WHERE id='$n'") or die(mysql_error() . "flap");
      if($query)
      {
        $news = mysql_fetch_array($query);
        $_comments .= '<table width="98%" cellpadding="0" cellspacing="0" CLASS="postingnews">';

        if(empty($page))
        {
          $page = "1";
        }

        $total = mysql_num_rows(mysql_query("SELECT * FROM `news_comments` WHERE `newsid`='$n'"));
        if($total < 1)
        {
          echo "No comments have been made yet.";
        }
        $total_pages = ceil($total / 10);
        //create pagination
        if($page < $total_pages)
        {
          $next = $page +1;
          $next = '<a class="comments" href="../process/news.php?n=' . $n . '&page=' . $next . '">Next</a>';
        }
        if($page > 1)
        {
          $prev = $page - 1;
          $prev = '<a class="comments" href="../process/news.php?n=' . $n . '&page=' . $prev . '">Previous</a>';
        }
        $offset = ($page - 1) * 10;
        $comments = mysql_query("SELECT * FROM `news_comments` WHERE `newsid`='$n' ORDER BY `id` ASC LIMIT $offset, 10");
        while($comment = mysql_fetch_assoc($comments))
        {
          $_comments .= '<tr><td colspan="2" valign="top" class="newsrepeat3" style="border-bottom:1px solid black;"><b>Added By: <a href="profile.php?username='.$comment['username'].'">'.$comment['username'].'</a> ';

        if($ui['rank'] == 'Senior Admin' || $ui['rank'] == 'Admin' || $ui['rank'] == 'Moderator')
            { $_comments .= ' <a href="news.php?n='.$n.'&d='.$comment['id'].'"> - Delete Comment</a>'; }

        $_comments .= '</b>
        <br>'.stripslashes($comment['message']).'</td></tr>';

        }
        if($_COOKIE['userid'] != "")
        {
          $text = '
            <form method="post" action="process/news.php?act=post" class="nyroModal">
                <textarea name="message" maxlength="200" style="width: 300px; height: 85px"></textarea><br />
                <input type="hidden" name="newsid" value="' . $n . '" />
                <input type="submit" value="reply" />
            </form>
          ';
        }
        $_comments .= '</table>';
        echo '
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
              ' . $css . '
            </head>
            <body style="margin: 0px; padding: 0px;">
        <div class="comments_div">
              <table cellspacing="0" cellpadding="0" border="0" width="98%">
              <tr><td colspan="2">  ' . $_comments . '</td></tr>
              <tr>
                <td align="left" width="49%">' . $prev . '</td>

                <td align="right" width="49%">' . $next . '</td>
              </tr>
              <tr><td colspan="2" align="center">
              ' . $text . '
              </td></tr>
              </table>
         </div>
            </body>
          </html>
        ';
      }
    }

    break;
  }
  case 'post':
  {
    //post news comment    
    $message = format($_POST[message]);
    if(!empty($message))
    {
      //check if underage
      if($ui[underage] == 'yes')
        { errormsg('Sorry but you are underage and cannot use this feature.', 'news.php', 'Go Back'); }
            $n = $_POST['newsid'];
      mysql_query("INSERT INTO `news_comments` (`newsid`, `username`, `message`) VALUES('$n', '$ui[username]', '$message') ");
      echo '<p align="center" style="font-weight: bold;">Thank you for your reply!</p>';
    }
    break;
  }
}
?>

然而,当我点击链接转到评论的下一页时,它什么也没做。我需要做些什么才能使用下一页评论重新加载模态?

1 个答案:

答案 0 :(得分:0)

您可以使用onComplete选项绑定下一页:

$("a.comments").fancybox({
    type: 'ajax',
    onComplete: function(){
        $("#fancybox-content a.comments").fancybox();
    }
});