使用jquery刷新动态php div

I print mysql result into div using php/while loops. now i need to refresh this result after click any link, button using jquery plugin or code . better design after click link, user see any loading message ( please wait ) and jquery/php print new result ( refresh div ). Is this possible? There is a way to do this?

My div is :

<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

To do what you want you'll need to use Ajax.

1.Create a separate PHP-file that contains your mysql result, the html-snippet that is created below.

Contents of 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.If you're using jQuery the ajax-request is pretty simple. Add the following to your current page (leave your current page otherwise as it is):

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

I haven't tested the code above but it should work.

Your script can't work. You are mixing PHP and HTML:

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

I think this is what you want:

Create a new PHP file which only outputs your list. Call it, for example, list.php.

Content of main file:

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

Content of 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 } ?>

Add this to the <head> part of the main file:

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

to load the content.

Add an onclick to your element:

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

create the 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); 
}