0

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

我的 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

4

3 回答 3

2

您的脚本无法运行。您正在混合 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>

加载内容。

于 2012-04-07T12:55:31.557 回答
2

添加一个onclick到你的元素:

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

创建javascript函数:

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); 
}
于 2012-08-28T22:19:11.997 回答
0

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

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

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-request非常简单。将以下内容添加到当前页面(否则保持当前页面不变):

$(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);    
            }
        });
    });
});

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

于 2012-04-07T12:54:22.860 回答