【问题标题】:refresh dynamic php div using jquery使用jquery刷新动态php div
【发布时间】:2012-04-20 17:27:22
【问题描述】:

我使用 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

【问题讨论】:

  • 其实我也不知道。我是 jquery 的业余爱好者。感谢您尝试了什么!

标签: php jquery mysql loading


【解决方案1】:

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

将此添加到主文件的&lt;head&gt; 部分:

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

加载内容。

【讨论】:

  • 我不需要这个方法:加载外部页面。使用同一页面。但感谢您的回答。
  • 没有第二个文件是不可能的。
【解决方案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); 
}

【讨论】:

    【解决方案3】:

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

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

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

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

    【讨论】:

    • 使用 jquery 外部加载/文档已准备好我的 jquery 库和其他代码在我的页面中加载时不适用于外部页面。
    • 我不完全明白你想说什么。 ajax 不适合您吗?您所做的只是将一些 javascript 添加到您的主文件并创建一个单独的 php 文件。
    • PHP 代码有错误。看看我的answer。
    • @Alex:你的意思是丢失的
      ?
        也不见了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 2011-03-06
    • 1970-01-01
    相关资源
    最近更新 更多