最奇怪的事情。如果我上传文件并开始单击链接,它就可以正常工作。但是,如果我在另一个选项卡上冲浪,然后返回它并单击链接,则 .post 或 .click jquery 似乎不起作用。我只包括了代码的摘录。如果您需要查看更多代码,请告诉我。谢谢!!
likeOnClick.php 文件
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript" src="includes/js/jquery.form.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".like").click(function() {
var data = $(this).attr('data-id');
$.post("updateLikes.php", {'id': data});
});
return false;
});
</script>
...
displayDBUsers.php
while($row = mysql_fetch_array($result))
{
?>
<?php $id = $row['uniqueid'];?>
<?php $finalID = "showdata" . $id ;?>
<?php echo "<br>FinalID: " . $finalID;?>
<?php echo "<br>ID: " . $row['uniqueid'];?>
<?php echo "<br>Name: " . $row['surname']; ?>
<?php echo "<br><a href class=like data-id=" . $id . ">Like (" . $row['likes'] . ")</a>"; ?>
<?php echo "<br><br>"; ?>
<?php
}
...
updateLike.php
if( $_POST['id'])
{
$unique_id = mysql_real_escape_string($_POST['id']);
$sql = "
UPDATE atable
SET likes=likes + 1
WHERE uniqueid=$unique_id;";
$result = mysql_query($sql);
if(!$result)
{
echo "Failed to retrieve record";
}