2016-06-07 36 views
0

你好负载我有负载的问题,更阿贾克斯都是好,但是当没有 数据从数据库显示告诉我这个错误:http://i.stack.imgur.com/6158Y.png更多与此阿贾克斯PHP

的index.php

<div id="show_here"> 
<div id="hide_here" onclick="show_more_posts(<?php echo $post['id']; ?>);">Show More Posts</div> 
</div> 

main.js

// Load more post 
function show_more_posts(post_id) 
{ 
    var ID = post_id; 
    $.ajax({ 
    type: "POST", 
    url: "ajax/ajax_more.php", 
    data: "lastmsg="+ ID, 
    cache: false, 
    success: function(html){ 
     $("#show_here").append(html); 
     $("#hide_here").remove(); 
    } 
    }); 
    return false; 
} 

ajax_more.php

<?php 
include("../config.php"); 

$lastmsg = $_POST['lastmsg']; 
$result = mysqli_query($Connection, "select * from posts where id<'$lastmsg' order by id desc limit 10"); 

while($row = mysqli_fetch_array($result)) 
{ 
    $msg_id=$row['id']; 
    $message=$row['text']; ?> 

    <li> <?php echo $message; ?> </li> <?php 
} 
?> 

<div id="show_here"> 
    <div id="hide_here" onclick="show_more_posts(<?php echo $msg_id; ?>);">Show More Posts</div> 
</div> 
+0

您必须使用“GET”方法,如果你使用'echo'作为输出检索数据核对总受影响的行数据。 –

+0

申报$ msg_id ='';在循环前 – kc1994

+1

你应该先定义$ msg_id而 – yafater

回答

0

你只需要做这样

<div id="show_here"> 
<?php 
if(isset($post['id'])) { 
?> 
    <div id="hide_here" onclick="show_more_posts(<?php echo $post['id']; ?>);">Show More Posts</div> 
<?php 
}else{ 
?> 
<div id="hide_here">No More Posts to show !!!!</div> 
<?php 
} 
?> 
</div> 

试试这个,它可以帮助你

+0

@ C0dekid我可以知道我的答案是无效的吗? – Siddharth

+0

@ C0dekid你的意思是标签?然后知道一个像你这样的人不能假定PHP标签投了我的答案lol – Siddharth

+0

坚持,让我编辑你的答案,比你能看出我的意思,如果你开始编辑,我将upvote您的答案,因为有人downvoted它由于您的答案中的一些错误:) – Jer

0

您可以使用这样的。那么你不会得到错误。

<?php 
if(isset($msg_id)) { 
?> 
<div id="show_here"> 
    <div id="hide_here" onclick="show_more_posts(<?php echo $msg_id; ?>);">Show More Posts</div> 
</div> 
<?php 
} 
?> 
0

在你ajax_more.php文件你有,而之前设置$msg_id$lastmsg

<?php 
include("../config.php"); 

$lastmsg = $_POST['lastmsg']; 
$result = mysqli_query($Connection, "select * from posts where id<'$lastmsg' order by id desc limit 10"); 

$msg_id = $lastmsg; 
while($row = mysqli_fetch_array($result)) 
{ 
    $msg_id=$row['id']; 
    $message=$row['text']; ?> 

    <li> <?php echo $message; ?> </li> <?php 
} 
?> 

    <div id="hide_here" onclick="show_more_posts(<?php echo $msg_id; ?>);">Show More Posts</div> 

所以,即使你的查询没有返回数据(没有新的消息),变量有正确的价值。

相反,如果你希望,在某些时候,有没有更多内容加载,你必须使用从卡娅Mydeen还提供了解决方案:

<?php 
include("../config.php"); 

$lastmsg = $_POST['lastmsg']; 
$result = mysqli_query($Connection, "select * from posts where id<'$lastmsg' order by id desc limit 10"); 

while($row = mysqli_fetch_array($result)) 
{ 
    $msg_id=$row['id']; 
    $message=$row['text']; ?> 

    <li> <?php echo $message; ?> </li> <?php 
} 
?> 

<?php if(isset($msg_id)) { ?> 
    <div id="hide_here" onclick="show_more_posts(<?php echo $msg_id; ?>);">Show More Posts</div> 
<?php } ?> 

而且,你的代码有事端奇怪:您继续添加divid="show_here"

0

我想取前通过while循环,你应该有这样的

$totrows = mysql_num_rows($result); 
if($totrows > 0){ 
    //your while loop for fetch data 
}else{ 
    //return something else 
}