0

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

1
  • Actually, in fact i dont have idea. im amateur in jquery. thanks for What have you tried ! Commented Apr 7, 2012 at 12:52

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

i dont need to this method : loading external pages. using same page. but thanks for answer.
it's not possible without a second file.
2

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

Comments

0

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.

4 Comments

using jquery external load/document is ready my jquery library and other code not work with external page when load in my page.
I don't fully understand what you're trying to say. Is ajax not an option for you? All you're doing is adding some javascript to your main file and creating a separate php file.
There is a mistake in the PHP code. Take a look at my answer.
@Alex: You mean the missing </div>? The <ul>s were also missing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.