【发布时间】:2012-02-02 16:37:53
【问题描述】:
我有一个通过 PHP 从 MySQL 数据库填充的数据表。第一列是一个标志功能,因此当单击任何特定行上的图钉图标时,该字段的数据库将更新为“y”或“n”。然后,我可以对该列进行排序,将我所有标记的行带到表的顶部。由于我的表相当大,我决定通过 Ajax 来实现。但是,它仅适用于第一个呼叫,然后变为非活动状态。第一次请求后数据库也不会更新。
这里是 HTML/PHP。
<td align="center" id="pin_record">
<?php // Flag Column
if ($row['flag'] == 'n'){ ?>
<a class="flag_image" href="list_all_quotes.php?id=<?php echo $row['quotes_id'].'_y'; ?>">
<img src="../images/flag_off.gif" border="0" width="17" height="17" alt="Flag Off">
<?php } elseif ($row['flag'] == 'y'){ ?>
<a class="flag_image" href="list_all_quotes.php?id=<?php echo $row['quotes_id'].'_n'; ?>">
<img src="../images/flag_on.gif" border="0" width="17" height="17" alt="Flag On">
<?php } ?>
</a>
</td>
这里是 jQuery
function ajax_init()
{
$('#pin_record a:first-child').on("click",(flag_record));
}
function flag_record()
{
var id_pin = this.href.replace(/.*=/,'');
var id_array = id_pin.split("_"); // split the quote id and pin
var id = id_array[0];
var pin = id_array[1];
this.id = 'flag_link_'+id;
$.getJSON('deletequote.php?ajax=true&id='+id+'&pin='+pin,set_flag);
return false;
}
function set_flag(data)
{
if(!data.success)return alert(data.serverMessage);
var href = '<a class="flag_image" href="list_all_quotes.php?id='+data.id+'_'+data.pin+'">';
$('#flag_link_'+data.id)
.replaceWith(href+data.flagimg);
}
$(document).ready(ajax_init);
我对使用 Ajax 还是很陌生,因此我们将不胜感激。
非常感谢, 克里斯
【问题讨论】:
标签: php jquery mysql ajax datatables