Php 为什么Ajax只适用于最新的帖子?

Php 为什么Ajax只适用于最新的帖子?,php,ajax,Php,Ajax,我想使用Ajax调用PHP页面而不刷新 这是我的密码 <?php global $db; $cmd = "SELECT * FROM posts order by date desc"; $result = mysqli_query($db,$cmd); while ($row = mysqli_fetch_assoc($result)){ $post_txt = $row['text']; $post_image = $row['image']; $post

我想使用Ajax调用PHP页面而不刷新

这是我的密码

<?php 


global $db;
$cmd = "SELECT * FROM posts order by date desc";
$result = mysqli_query($db,$cmd);
while ($row = mysqli_fetch_assoc($result)){
    $post_txt = $row['text'];
    $post_image = $row['image'];
    $postid = $row['postid'];
    $date = $row['date'];
    $userid = $row['userid'];

    $new_date=time_elapsed_string($date);


    ?>


    <?php
    global $db;
    $sql = "SELECT * FROM user WHERE id = '$userid'";
    $result2 = mysqli_query($db,$sql);
    while($row2 = mysqli_fetch_assoc($result2)){
        $acountname = $row2['name'];
        $acountlast_name = $row2['lastname'];
    } 
    ?>


<?php



echo"
<div id='postShare'>

    <div class='col-xs-7' id='listPost'>
                <div class='list-group'>    

            <input type='hidden' value='$userid' id='user_id_input_hidden'>     
            <li class=list-group-item text-center'><span class='pull-right'><p><a href='profile.php' id='prof_btn'>$acountname  $acountlast_name</a></p></span>&nbsp;
            <span class='pull-left'><p>$new_date</p></span>
            <hr>
            <center><h5 id='show_post_title'>$post_txt</h5></center> <hr>";?>

但它只适用于echo中的最新帖子,不适用于任何帖子。有什么问题吗?

加载配置文件时,添加事件的prof_btn元素将消失,并替换为不绑定任何事件的新元素

您有两个选择:

手动重新应用事件。 用于将事件委托给未被替换的父元素。
加载概要文件时,添加事件的prof_btn元素将消失,并替换为不绑定任何事件的新元素

您有两个选择:

手动重新应用事件。 用于将事件委托给未被替换的父元素。
将回音移到环路内

global $db;
$sql = "SELECT * FROM user WHERE id = '$userid'";
$result2 = mysqli_query($db, $sql);
while ($row2 = mysqli_fetch_assoc($result2)) {
    $acountname = $row2['name'];
    $acountlast_name = $row2['lastname'];
    echo"
        <div id='postShare'>
        <div class='col-xs-7' id='listPost'>
        <div class='list-group'>    
        <input type='hidden' value='$userid' id='user_id_input_hidden'>     
        <li class=list-group-item text-center'><span class='pull-right'><p><a href='profile.php' id='prof_btn'>$acountname  $acountlast_name</a></p></span>&nbsp;
        <span class='pull-left'><p>$new_date</p></span>
        <hr>
        <center><h5 id='show_post_title'>$post_txt</h5></center> <hr> 
        ";
} 

将回音移到环路内

global $db;
$sql = "SELECT * FROM user WHERE id = '$userid'";
$result2 = mysqli_query($db, $sql);
while ($row2 = mysqli_fetch_assoc($result2)) {
    $acountname = $row2['name'];
    $acountlast_name = $row2['lastname'];
    echo"
        <div id='postShare'>
        <div class='col-xs-7' id='listPost'>
        <div class='list-group'>    
        <input type='hidden' value='$userid' id='user_id_input_hidden'>     
        <li class=list-group-item text-center'><span class='pull-right'><p><a href='profile.php' id='prof_btn'>$acountname  $acountlast_name</a></p></span>&nbsp;
        <span class='pull-left'><p>$new_date</p></span>
        <hr>
        <center><h5 id='show_post_title'>$post_txt</h5></center> <hr> 
        ";
} 

拆下此处的最后一个支架:

while($row2 = mysqli_fetch_assoc($result2)){
    $acountname = $row2['name'];
    $acountlast_name = $row2['lastname'];
---> } 
最后在这里写上:

<center><h5 id='show_post_title'>$post_txt</h5></center> <hr>";
--> }?>

拆下此处的最后一个支架:

while($row2 = mysqli_fetch_assoc($result2)){
    $acountname = $row2['name'];
    $acountlast_name = $row2['lastname'];
---> } 
最后在这里写上:

<center><h5 id='show_post_title'>$post_txt</h5></center> <hr>";
--> }?>

如果要对每个结果行执行echo命令,则需要将其放入while循环中。每次循环while循环时,都会覆盖$acountname和$accountlast\u名称。我很困惑,你说要返回所有帖子,而不仅仅是最新的帖子,而你的sql语句只获取配置文件信息。从带有id的ajax帖子的外观来看,只获得一条记录也就不足为奇了。你能编辑你的问题并澄清你想要达到的目标吗,您有许多未定义的变量$post_txt和$new_date例如,伙计们,很抱歉,我在while和echo工作时编辑了我的问题,但我想在用户单击链接名pro_btn名为profile.php的页面时使用ajax加载,但它仅适用于最新的帖子,而不是所有帖子如果您想对每个结果行执行echo命令,你需要将其放入while循环中。每次循环while循环时,你都会覆盖$acountname和$accountlast\u名称。我很困惑,你说你想返回所有帖子,而不仅仅是最新的,但你的sql语句只获取配置文件信息。从带有id的ajax帖子的外观来看,只获得一条记录也就不足为奇了。你能编辑你的问题并澄清你想要达到的目标吗,您有许多未定义的变量$post_txt和$new_date例如,伙计们,很抱歉,我在while和echo工作时编辑了我的问题,但我想在用户单击ad链接名pro_btn名为profile.php的页面时使用ajax加载,但它仅适用于最新的帖子,而不是所有帖子