繁体   English   中英

使用 jquery 刷新动态 php div

[英]refresh dynamic php div using jquery

我使用 php/while 循环将 mysql 结果打印到 div 中。 现在我需要在点击任何链接后刷新这个结果,按钮使用 jquery 插件或代码。 单击链接后更好的设计,用户看到任何加载消息(请稍候)和 jquery/php 打印新结果(刷新 div)。 这可能吗? 有办法做到这一点吗?

我的股利是:

<a class="click" href="#"> Link TO refresh Div </a>
<div class="messagelist">
<?PHP $result=mysql_query("select * from messages where id<'$lastmsg' order by id desc limit 20");
$count=mysql_num_rows($result);
<div class="commentbox">
while($row=mysql_fetch_assoc($result))
{?>
<li>
<?php echo $row['id'] . ' #' . $row['date'] . ' / ' . $row['comment']; ?>
</li>
<?PHP } ?>
</div>
</div>

NOTE: I dont need To Load From Jquery External File Methods. thanks

您的脚本无法运行。 您正在混合 PHP 和 HTML:

$count=mysql_num_rows($result);
<div class="commentbox"> /*THIS IS WRONG*/
while($row=mysql_fetch_assoc($result))

我想这就是你想要的:

创建一个新的 PHP 文件,它只输出您的列表。 例如,将其命名为list.php

主文件内容:

<a class="click" href="#"> Link TO refresh Div </a>
<div class="messagelist">
<div class="commentbox">
<ul>
<?PHP $result=mysql_query("select * from messages where id<'$lastmsg' order by id desc limit 20");
$count=mysql_num_rows($result);
while($row=mysql_fetch_assoc($result))
{?>
<li>
<?php echo $row['id'] . ' #' . $row['date'] . ' / ' . $row['comment']; ?>
</li>
<?PHP } ?>
</ul>
</div>
</div>

list.php的内容:

<?PHP $result=mysql_query("select * from messages where id<'$lastmsg' order by id desc limit 20");
$count=mysql_num_rows($result);
while($row=mysql_fetch_assoc($result))
{?>
<li>
<?php echo $row['id'] . ' #' . $row['date'] . ' / ' . $row['comment']; ?>
</li>
<?PHP } ?>

将其添加到主文件的<head>部分:

<script type="text/javascript">
$(function(){
    $('.click').on('click', function(e){
        e.preventDefault();
        $('.messagelist').text('Please wait...');
        $('.messagelist').load('list.php');
    });
});
</script>

加载内容。

onclick添加到您的元素中:

<a onclick="loadPhp();">Reload php function</a>

创建javascript function:

function loadPhp(){
    //set a variable for the php function
    var func = <?php yourphpfunction();?>;
    //append the php function results to a div using jquery
    $(element).html(func); 
}

要做你想做的事,你需要使用 Ajax。

1.创建一个单独的 PHP 文件,其中包含您的 mysql 结果,即下面创建的 html 代码段。

messages.php的内容:

<?PHP $result=mysql_query("select * from messages where id<'$lastmsg' order by id desc limit 20");
$count=mysql_num_rows($result);
<div class="commentbox">
<ul>
while($row=mysql_fetch_assoc($result))
{?>
<li>
<?php echo $row['id'] . ' #' . $row['date'] . ' / ' . $row['comment']; ?>
</li>
</ul>
</div>
<?PHP } ?>

2.如果您使用的是 jQuery, ajax 请求非常简单。 将以下内容添加到您的当前页面(否则请保持当前页面不变):

$(document).ready(function () {
    $('.click').on('click', function (e) {
        e.preventDefault();
        $('.messagelist').html('Please wait...');
        $.ajax({
            type : 'GET',
            url : 'messages.php',
            dataType : 'html',
            success : function (response) {
                $('.messagelist').html(response);    
            }
        });
    });
});

我没有测试上面的代码,但它应该可以工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM