简体   繁体   English

如何使用ajax php刷新div标签内的表

[英]How to refresh a table inside a div tag using ajax php

I have an ajax table click event that gets database data according to the clicked name from the first table and display the second table with employee information. 我有一个ajax表单击事件,该事件根据第一个表中的单击名称获取数据库数据,并显示第二个表以及员工信息。 The problem now is that the employee information data only pile up in the second table, I want to refresh the second table every name that the user clicked. 现在的问题是,员工信息数据仅堆积在第二个表中,我想刷新用户单击的每个名称的第二个表。 By the way the second table is inside a div tag. 顺便说一句,第二个表位于div标签内。 Here is my ajax code: 这是我的ajax代码:

function putThis(control){
var getid = control.innerText;
var first = $("#from[name=from]").val();
var second = $("#to[name=to]").val();
$.ajax({
    type:'POST',
    url: 'testtable.php',
    data: {id: getid,from: first,to: second},
    cache: false,
    global: false,
    success:function(data)
    {
    $("#result").append(data);
    alert("Success: " + getid + " " + first + " to " + second);
    }
});
}

If I am reading your question properly (and correct me if I am not), you want to replace the contents of the element rather than append the data to the element. 如果我正确阅读了您的问题(如果我没有阅读,请纠正我),您想替换元素的内容,而不是将数据附加到元素。

This can be accomplished with your code by changing this line : 可以通过更改以下代码来用代码完成此操作:

$("#result").append(data);

To this : 对此:

$("#result").html(data);

append will put data at the end of the element before the closing tag, while html will consume the element with the data you are inserting. append会将数据放在结束标记之前元素的末尾,而html会将元素与您要插入的数据一起使用。

In your code as I can see you want to click on user name from left panel and call ajax for his/her information from database . 如您所见,在您的代码中,您想单击左侧面板上的用户名,并从数据库调用ajax获取其信息。 But as you are appending that information to div and this append call append the data a the end of the div instead you can call $("#result").html(data); 但是,当您将信息追加到div时,此append调用会将数据追加到div的末尾,而您可以调用$("#result").html(data); . So final ajax call will be- 因此,最终的ajax调用将是-

$.ajax({
    type:'POST',
    url: 'testtable.php',
    data: {id: getid,from: first,to: second},
    cache: false,
    global: false,
    success:function(data)
    {
    $("#result").html(data);
    alert("Success: " + getid + " " + first + " to " + second);
    }
});

if still issue persists you can do following steps - 1. check any error in console tab in firebug. 如果问题仍然存在,您可以执行以下步骤-1.在Firebug的控制台标签中检查任何错误。 2. if ajax is failing at any point . 2. ajax是否在任何时候失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM