当谈到PHP,Javascript和AJAX时,我很新,很抱歉,如果这是一个简单的问题。我正在开发插件;在这个插件中,我正在用PHP开发一个表格。用户可以将链接添加到特定的小部件以显示在其主页上。我正在尝试使用Javascript和AJAX创建一个从列表和数据库中删除一行的函数。我想分配给每个TR一个唯一的ID,但我不确定如何做到这一点。这里是我的代码:使用JQuery和PHP删除表格行
<table id="links" width="350px" border="1">
<tr>
<td></td>
<td><b>Display</b></td>
<td><b>Link *</b></td>
<?php
if (isset($_POST['links'])) {
$displays = $_POST['displays'];
$links = $_POST['links'];
$domain = $_POST['domain'];
foreach($links as $i => $link) {
if (empty($displays[$i]))
$displays[$i] = "";
if(empty($links[$i]))
unset($links[$i]);
}
$autodomain = array('displays' => $displays, 'links' => $links, 'domain' => $domain);
$options = get_option("autodomain_links") or array();
$options[] = $autodomain;
//$dump = array();
update_option("autodomain_links", $options);
}
$options = get_option("autodomain_links");
if ($options) {
foreach($options as $option) if ($tmp++ < 15) {
$displays = $option['displays'];
$links = $option['links'];
$domain = $option['domain'];
foreach($links as $i => $link)
{
echo '<tr>';
echo '<td><a href="#" class="delete"/><img alt="" border="0" src="https://jarkin.its-express-tst.syr.edu/wp-content/uploads/2011/06/delete.png" /></a></td>';
echo '<td>' . $displays[$i] . '</td>';
echo '<td>' . $link . ' ' . $domain . '</td>';
echo '</tr>';
;
}
}
}
?>
</table>
这里是JavaScript/AJAX:
<script type="text/javascript" >
$(document).ready(function()
{
$('table#links td a.delete'').click(function()
{
if (confirm("Are you sure you want to delete this row?"))
{
var id = $(this).parent().parent().attr('id');
var data = 'id=' + id ;
var parent = $(this).parent().parent();
$.ajax(
{
type: "POST",
url: "delete_row.php",
data: data,
cache: false,
success: function()
{
parent.fadeOut('slow', function() {$(this).remove();});
}
});
}
});
});
</script>
的JavaScript/AJAX正在使用表ID和TR ID作为父母来标识行 - 但由于我使用PHP,我不知道如何为每个TR生成唯一的ID。
当我尝试这样每个TR没有收到一个独特的价值,有几个1S + 0 – Jamie
@Jamie,你通过这个循环多少次?如果你只走了一次,你应该有独特的ID – Neal
我的插件是一种奇怪的 - 它的一个小部件,允许用户添加链接到列表,而不必键入域名,所以每次他们进入一个新的链接,它是制作一个新阵列。我能够删除每一行,但它不会从wordpress数据库 – Jamie