使用Ajax请求单击链接后更改div的内容

时间:2014-01-08 20:21:54

标签: javascript php html jquery

我的div包含一个带有sql查询的PHP函数,该查询从threads表中获取最新的线程。我的页面结构是这样的;

Section # 1 --- Link 1
Section # 2 --- Link 2
Section # 3 --- Link 3

我想要做的就是像点击Link 1时那样显示来自Section 1的最新主题,当点击Link 3时,它会显示{的最新主题{1}}。

请注意:我知道我可以使用slidetoggle()jQuery函数来显示和隐藏div,但我想让它单击WHEN链接然后运行sql查询以显示最新的线程。我正在使用以下jQuery;

Section 3

我的PHP文件jQuery(document).ready(function($) { $('a[id^="forum_name"]').on('click', function (e) { e.preventDefault(); var fid = $(this).attr("fid"); $.ajax( { type: "POST", url: 'latest_threads.php?fid='+fid, dataType: 'json', success: function (data) { $("#forum_threads_"+fid).html(data).stop().slideToggle("fast"); } }); }); }); 包含以下代码;

latest_threads.php

和我的HTML就像;

<?php
define("IN_MYBB", 1);
require_once "./global.php";

if ($mybb->input['fid'] != "")
{
    require_once MYBB_ROOT."inc/functions.php";
    $fid = intval($mybb->input['fid']);
    $forum['forum_threads'] = kalachi_forum_threads($fid);
}
?>

但它不起作用,请帮助!

3 个答案:

答案 0 :(得分:1)

就我所见,你没有输出你的回复。

尝试这样的事情:

...
$forum['forum_threads'] = kalachi_forum_threads($fid);
echo $forum['forum_threads']
exit();
...

我添加了 echo 行,将实际响应输出回浏览器。

答案 1 :(得分:1)

jQuery的:

jQuery(document).ready(function($){
  $('a[id^="forum_name"]').on('click', function (e){
        e.preventDefault();
        var fid = $(this).attr("fid");
        $.ajax(
        {
            type : "post",
            dataType: "html",
            url : "misc.php?action=forum_threads&fid="+fid,
            cache: false,
            success : function(response)
            {
                $("#forum_threads_"+fid).stop().slideToggle("fast").html(response);
            }
        });
  });
});

PHP:

<?php
define("IN_MYBB", 1);
require_once "./global.php";

if ($mybb->input['fid'] != "")
{
    require_once MYBB_ROOT."inc/functions.php";
    $fid = intval($mybb->input['fid']);
    echo $forum['forum_threads'] = kalachi_forum_threads($fid);
    exit;
}
?>

在HTML中:

将最后一行更改为:

<div id="forum_threads_{$forum['fid']}"></div>

答案 2 :(得分:0)

您可以阅读Ajax教程。

但在您的情况下,您应该知道,您在代码的这一部分data中显示的div

success: function (data)
{
   $("#forum_threads_"+fid).html(data).stop().slideToggle("fast");
}

是您的PHP代码的响应。

您的php响应是您的PHP代码的任何输出,它可以是简单的echo 'something'或HTML代码,或包含html文件等。

生成一个html代码或在你的php代码中包含一个html文件,你希望在你的div中显示任何内容。

注意:始终在您的php代码的末尾添加die()exit()

这是一个简单的例子:

HTML

<div id="c1" onclick="javascript:loadcontent(1);">click</div><br>
<div id="c2" onclick="javascript:loadcontent(2);">click</div><br>
<div id="c1" onclick="javascript:loadcontent(3);">click</div><br/

Javascript(jquery)

<script>
    function loadcontent(id) {
        $.post("yourphp.php", {id: id}, function(data) {
            if (data) {
                $("#c" + id).html(data);
            }
            else
            {
                alert("error");
            }
        });
    }
</script>

PHP - yourphp.php

<?php

$id = $_POST['id'];
require 'filename'.$id.".html";
die;
?>

然后您必须创建3个html文件,其名称为: filename1.html filename2.html filename3.html 并在其中放置您想要在id为c1,c2或c3的div中显示的任何内容。

多数民众赞成

相关问题