在php中使用jquery ajax更新记录

在php中使用jquery ajax更新记录,php,jquery,Php,Jquery,我已在表格中显示记录,并具有编辑链接,当我单击该链接时,该行的值将显示在文本框中,但当我单击按钮时,它将插入新记录,而不是更新表格中的记录。我应该对更新记录做些什么更改 comment.php <script type="text/javascript"> $(document).ready(function(){ function loadList(){ $.ajax({ url: "load_list.php",

我已在表格中显示记录,并具有编辑链接,当我单击该链接时,该行的值将显示在文本框中,但当我单击按钮时,它将插入新记录,而不是更新表格中的记录。我应该对更新记录做些什么更改

comment.php

<script type="text/javascript">
$(document).ready(function(){
    function loadList(){
        $.ajax({
            url: "load_list.php",
            cache: false,
            success : function(html){
                $(".name_list").html(html);
            }
        });
    }
    loadList();

    $("#Submit").click(function() {
        if($(":text").val().length==0)
            {
               // $(this).next().html("Field needs filling");
               $(":text").after('<span class="errorkeyup">Field cannot be empty</span>');
                //return false;
                success = false;
            }
            else
            {
                var name=$("#name").val();
                var message=$("#message").val();
                $.ajax({
                  type:"post",
                   url:"save_list.php",
                   data:"name="+name+"&message="+message,                             
                  success:function(data){
                 loadList();                                
                 } 
                }); 
                return false;
            }
    });

    $("a.delete_button").on("click", function(e){
        //this deletes the row clicked on with an alert and then reloads the list
        e.preventDefault();
        //var id = $(this).attr("id");
        var obj = $(this); 
        var id = obj.attr("id");
            $.ajax({
                type: "POST",
                url: "delete.php",
                data: "id="+ id,
                success: function(response){
                if(response == '1') {
                      obj.parent.remove();
                }
            }
            });      
        return false;
    });

    $("a.edit_button").click(function(){
    //$("form#update").live("submit",(function() {
    // we want to send via ajax and empty the html from the update_form element
        var name = $('#name').attr('value');
        var message = $('#message').attr('value');
        var id = $('#id').attr('value');
        $("#wrapper:has(button)").hide();
       $("#Submit").click(function(){
            $.ajax({

                url: "updateajax.php",
                type: "POST",
                data: "name="+ name +"& message="+ message +"& id="+ id,
                error: function(){
                    alert('Error loading document');
                             },
                success: function(){
                alert (" i  am  in success below load list ");
                    $(".update_form").empty();
                    //loadList();

                        }

            });
    });
        return false;
    });

});
</script>
</head>

<body>


<form method="post" name="form" action="">
<div id="wrapper" align="center">
<?php
include('connection.php');
if(isset($_GET["id"]))
{
    $cmd=mysql_query("select * from login where id=".$_GET["id"]);
                $rs=mysql_fetch_array($cmd);
}
?>
<table width="200" border="1">
  <tr>
    <td colspan="2">Form:<input type="hidden" name="idupdate" id="idupdate" value="<?php echo $rs[0];?>"/></td>

  </tr>
  <tr>
    <td>Name:</td>
    <td><input type="text" name="name" id="name" value="<?php echo $rs[1];?>"  /></td>
  </tr>
  <tr>
    <td>Message:</td>
    <td><input type="text" name="message" id="message" value="<?php  echo $rs[2]; ?>"  /></td>
  </tr>
  <tr>
    <td colspan="2" align="center"><input type="button" value="Submit" id="Submit"><input type="button" value="edit" id="edit" style="display:none"></td>
    </tr>
</table>
 </div>
<div class="name_list" align="center"></div>
 <div class="update_form" style="display:none">

 </div>
 </div> 
 </form>
</body> 

$(文档).ready(函数(){
函数loadList(){
$.ajax({
url:“load_list.php”,
cache:false,
成功:函数(html){
$(“.name_list”).html(html);
}
});
}
loadList();
$(“#提交”)。单击(函数(){
if($(“:text”).val().length==0)
{
//$(this.next().html(“字段需要填充”);
$(“:text”).after('字段不能为空');
//返回false;
成功=错误;
}
其他的
{
var name=$(“#name”).val();
var message=$(“#message”).val();
$.ajax({
类型:“post”,
url:“save_list.php”,
数据:“name=“+name+”&message=“+message,
成功:功能(数据){
loadList();
} 
}); 
返回false;
}
});
$(“a.delete_按钮”)。在(“单击”上,函数(e){
//这将删除通过警报单击的行,然后重新加载列表
e、 预防默认值();
//var id=$(this.attr(“id”);
var obj=$(本);
变量id=对象属性(“id”);
$.ajax({
类型:“POST”,
url:“delete.php”,
数据:“id=”+id,
成功:功能(响应){
如果(响应='1'){
obj.parent.remove();
}
}
});      
返回false;
});
$(“a.编辑按钮”)。单击(函数(){
//$(“表单更新”).live(“提交”,函数(){
//我们希望通过ajax发送,并从update_表单元素中清空html
var name=$('#name').attr('value');
var message=$('#message').attr('value');
变量id=$('#id').attr('value');
$(“#包装器:有(按钮)”).hide();
$(“#提交”)。单击(函数(){
$.ajax({
url:“updateajax.php”,
类型:“POST”,
数据:“name=“+name+”&message=“+message+”&id=“+id,
错误:函数(){
警报(“加载文档时出错”);
},
成功:函数(){
警报(“我在负载列表下方成功”);
$(“.update_form”).empty();
//loadList();
}
});
});
返回false;
});
});

表单:更改loadlist.php以以下格式返回链接

echo"<td>
                 <a id='".$row['id']."' href='comment.php?id=".$row['id']."&type=edit' class='edit_button'>Edit</a></td>";
echo”
";
echo”
"
在上面的行set href=“#”,因为您实现了此锚定标记的单击事件。如果您在

<a id=".$row['id']." href=comment.php?id=".$row['id']."&type=edit class=edit_button>Edit</a>

然后,它将转到href中指定的url,而不是单击函数。

而不是使用

echo"<td>
             <a id=".$row['id']." href=comment.php?id=".$row['id']."&type=edit class=edit_button>Edit</a></td>";
echo”
";
您可以使用一个按钮,因为不管怎样,只要点击这个锚,您就会调用ajax

因此,按照Katuli的规定,向锚定标记添加href只会将您引导到该页面


因此,请尝试她的设置href='#'的建议,或者使用除锚定标记以外的任何内容。

清除您的问题…正确提问。。。
echo"<td>
                 <a id=".$row['id']." href=comment.php?id=".$row['id']."&type=edit class=edit_button>Edit</a></td>"
<a id=".$row['id']." href=comment.php?id=".$row['id']."&type=edit class=edit_button>Edit</a>
echo"<td>
             <a id=".$row['id']." href=comment.php?id=".$row['id']."&type=edit class=edit_button>Edit</a></td>";