duangu9666 2013-06-11 02:06
浏览 52
已采纳

AJAX更新删除表中的tr:后端工作但前端出现故障

The purpose of the script below is to allow users to post new updates and to delete them without the page refreshing : that's why i'm using AJAX.

There are 2 glitches:

  • when a user posts an update, it's correctly saved in the DB, and the correct update slides down in my table but when i look at the source code, i can see the wrong postid is echoed (it's always the postid from a couple rows below in the DB).

  • when a user deletes a post, nothing happens on the frontend: tr doesn't slide up BUT the row does get deleted correctly in the DB.

PHP part :

echo "<table id=\"update_list\">
    <tr class=\"cell$postid\">
          <td>$post

      <div id=\"delete\">
          <span class=\"delete_update\"><a href=\"#\" id=\"$postid;\">X</a></span>  
          </div>  
          <hr>    
          </td>
    </tr>
  </table>";

AJAX part:

<script type="text/javascript">
$(function() {

    $("#addpost_button").click(function() 
        {
            var element = $(this);
            var boxval = $("#status").val(); // #status is the ID of the input where the users type in updates 
            var dataString = 'post='+ boxval;
            if(boxval=='')
            {
                alert("Please Enter Some Text");
            }
            else
            {
                $("#flash").show();

                $.ajax({
                    type: "POST",
                    url: "update_post.php",
                    data: dataString,
                    cache: false,
                    success: function(html){

                        $("#cell").prepend(html);
                        $("#update_list tr:first").slideDown("slow");
                        document.getElementById('post').value='';
                        $("#flash").hide();
                    }
                });
            }
            return false;
        });

    $('.delete_update').live("click",function() 
        {
            var ID = $(this).attr("id");
            var dataString = 'postid='+ ID;
            if(confirm("Sure you want to delete this post? "))
            {
                $.ajax({
                    type: "POST",
                    url: "delete_post.php",
                    data: dataString,
                    cache: false,
                    success: function(html){
                        $(".cell"+ ID).slideUp('slow', function() {$(this).remove();});
                    }
                });
            }
            return false;
        });
});
</script>

Any help will be much appreciated!

EDIT: Code from update_post.php:

<?php
include "includes/config.php";
if(isset($_POST['post']))


$table=query("INSERT INTO postlist (id, postid, post) VALUES (?, ?, ?)",         
$_SESSION["id"], '', $_POST["post"]);
$table = query("SELECT post, postid FROM postlist WHERE id = ? ORDER BY postid DESC",   
$_SESSION["id"]);

foreach ($table as $row){
   $post=$_POST['post'];
   $postid = $row["postid"];



echo "<table id=\"update_list\">
<tr class=\"cell$postid\">
  <td>$post

    <div id=\"delete\">
    <span class=\"delete_update\"><a href=\"#\" id=\"$postid;\">X</a></span>  
    </div>  
  <hr>    
  </td>
</tr>
</table>";     
} 
?>
  • 写回答

2条回答 默认 最新

  • dpbe81245 2013-06-11 02:33
    关注

    you have to repalce

     <span class=\"delete_update\"><a href=\"#\" id=\"$postid;\">X</a></span>
    

    to

     <span class=\"delete_update\"><a href=\"#\" id=\"$postid\">X</a></span> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建