如何根据我的AJAX结果主动增加我的计数器?(How do I actively increment my counter based off of my AJAX results?)
我正在进行我的第一个ajax项目的最后一步。 我做了一个竖起大拇指图标,当按下这个代码时,数据库中的一列将增加:
这是浏览页面上的HTML和JQUERY
<div id="comment_id">+1</div> <div id="thumb_thumb"> <?php $comment_id = $result['id'];?> <a class="myButtonLink" href="Profile_test.php?id=<?php echo $prof->id; ?>" id="<?php echo $comment_id; ?>">Vote Up!</a> </div> <script> $('.myButtonLink').click(function(e) { e.preventDefault(); var comment_id = $(this).attr('id'); $.ajax({ type: 'POST', url: 'thumbs.php', data: 'comment_id=' + comment_id, success: function(data) { alert(data); if(data.result == "error") { alert(data.msg); } else { $('#numvotes').html(data.msg); } } }); }); </script>这里是它隐藏的PHP页面
<?php require_once($_SERVER['DOCUMENT_ROOT'] . '/includes/system/init.php'); // 1. CHECK AND SEE IF THE "$comment_id" IS VALID. I AM GOING TO RETREIVE THE VALUE OF THE $_POST BEING SENT FROM THE PHP PAGE THAT IS SENDING THE REQUEST /* QUERY TO CHECK $_POST DATA WITH: */ /* this is grabbing id that jquery sent over via post */ if(isset($_POST['comment_id'])) { /* making a variable out of the grabbed id */ $retreived_comment_id = $_POST['comment_id']; $query = "UPDATE `CysticAirwaves` SET `thumbsUp` = `thumbsUp` + 1 WHERE `id` = '" . $retreived_comment_id . "'"; $request = mysql_query($query,$connection) ; $result = mysql_fetch_array($request); } ?>所以现在我只需要能够动态地让我的计数器工作,当一个拇指被点击并且指定的注释在db中被标记为加号
<div id="comment_id"> +1 //NEED TO MAKE THIS ACTUALLY COUNT </div>提前致谢
I am on the final step of my first ajax project. I've made a thumbs up icon that when pressed increments a column in the database via this code:
HERE IS THE HTML AND JQUERY ON THE VIEWABLE PAGE
<div id="comment_id">+1</div> <div id="thumb_thumb"> <?php $comment_id = $result['id'];?> <a class="myButtonLink" href="Profile_test.php?id=<?php echo $prof->id; ?>" id="<?php echo $comment_id; ?>">Vote Up!</a> </div> <script> $('.myButtonLink').click(function(e) { e.preventDefault(); var comment_id = $(this).attr('id'); $.ajax({ type: 'POST', url: 'thumbs.php', data: 'comment_id=' + comment_id, success: function(data) { alert(data); if(data.result == "error") { alert(data.msg); } else { $('#numvotes').html(data.msg); } } }); }); </script>HERE IS THE HIDDEN PHP PAGE IT IS SENT TO
<?php require_once($_SERVER['DOCUMENT_ROOT'] . '/includes/system/init.php'); // 1. CHECK AND SEE IF THE "$comment_id" IS VALID. I AM GOING TO RETREIVE THE VALUE OF THE $_POST BEING SENT FROM THE PHP PAGE THAT IS SENDING THE REQUEST /* QUERY TO CHECK $_POST DATA WITH: */ /* this is grabbing id that jquery sent over via post */ if(isset($_POST['comment_id'])) { /* making a variable out of the grabbed id */ $retreived_comment_id = $_POST['comment_id']; $query = "UPDATE `CysticAirwaves` SET `thumbsUp` = `thumbsUp` + 1 WHERE `id` = '" . $retreived_comment_id . "'"; $request = mysql_query($query,$connection) ; $result = mysql_fetch_array($request); } ?>So now I just need to be able to dynamically have my counter work when a thumb is clicked and the specified comment is marked a plus one in the db
<div id="comment_id"> +1 //NEED TO MAKE THIS ACTUALLY COUNT </div>Thanks in advance
最满意答案
var currentCount = $('#comment_id').text(); $('#comment_id').text(++currentCount); var currentCount = $('#comment_id').text(); $('#comment_id').text(++currentCount);更多推荐
发布评论