开发者

How do I store incremented votes in my database via AJAX?

开发者 https://www.devze.com 2023-03-08 12:56 出处:网络
I am on the final leg of my very first AJAX project. I have a social network that has comments. I am simply adding a thumb icon, that when pressed, sends the id of the comment via JQUERY to a backgrou

I am on the final leg of my very first AJAX project. I have a social network that has comments. I am simply adding a thumb icon, that when pressed, sends the id of the comment via JQUERY to a background php page that is SUPPOSED to store the id of the comment as well as a record of it being pressed ( increment ). After that happens, I need it to send a record of that back to the page the thumb icon is on, and tell that page that the thumb has been hit and increment the counter in the designated area.

In the thumb_incrementtable I have:

id that is auto incremented and comment_id that is the id of the comment that is being upvoted.

I am wondering if I should add another column to hold the amount of upvotes or just keep track in the comment_id column. I am a little confused on the logic there.

So far I am able to click the thumb and have it send over the id from my other page and grab the id via $_POST. Here is that code:

<?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']); 

/* this query is bring to frutation the exact id of the comment */
$query = "SELECT * FROM `CysticAirwaves` WHERE `id` = '".$retreived_comment_id."' && `status` = 'active'"; 
$request = mysql_query($query,$connection);
if($result = mysql_fetch_array($request)) {

/* insert the comment into the increment table */

$query = "INSERT INTO `thumb_increment` (
                                `comment_id`
                            ) VALUES (
                            '" . $retreived_commen开发者_C百科t_id . "'
                                )";
mysql_query($query,$connection);

/* increment the vote in the db */

    }

}


?>

So to summarize what I have left to do:

I need to increment and store the comment in my db, put that in a varaible that is sent back to the page that has the thumb icon on it, and have that variable tack on a increment to the counter

Thanks in advance.


Edit

Regarding just the database portion, per your comment...

I'd just have the normalized database for upvote contain:

comment_id, user_who_voted_id, timestamp

In the future if you want to have the concept of a downvote, like stackoverflow, you might need to add another column. In the former simpler case, to get the count, you're just doing something like:

SELECT count(*) FROM thumb_increment WHERE comment_id = ?

Old Answer

Wouldn't use $.get for something such as this as you're modifying state. I'd probably use a post. To rewrite his example...

http://api.jquery.com/jQuery.post/

http://api.jquery.com/serialize/

http://api.jquery.com/parent/

// Assuming html that looks something like the following
<form id='comment_details_123123'>
    <input type='hidden' value='123123' name='comment_id' />
    <div class='thumb'><img src='thumb.jpg' /></div>
    <div class='thumb_counter'>3</div>
</form>

$('.thumb').click(function(){
    var comment_form = $(this).parent('form');
    $.post('comment.php', comment_form.serialize(), function(response) {
        comment_form.find('.thumb_counter').html(response.new_count);
    });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消