Ajax调用只更新一次
我有一个通过 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 还很陌生,所以任何帮助将不胜感激。
非常感谢, 克里斯
I have a data table populated from a MySQL database via PHP. The first column is a flag feature so when the pin icon on any particular row is clicked the database is updated to either 'y' or 'n' for this field. I am then able to sort on this column bringing all my flagged rows to the top of the table. Since my table is rather large, I decided to do this via Ajax. However, it only works for the first call then becomes inactive. The database is not updated after the first request either.
here is the 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>
and here is the 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);
I am quite new to using Ajax so any help would be greatly appreciated.
Many thanks,
Kris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的请求正在被缓存。使用唯一的 URL 破坏缓存:
You request is being cached. Break the cache by using a unique URL: