Friday, November 16, 2012

javascript – Implementing a like/dislike counter with php and jquery

I have a comics website, and I\’d like to keep track of how many people click like and dislike for a single comic id… I\’d like to store the value in my database, and also report it back out to the browser.


This is my non-working solution:


viewcomic.php:



<script type=\"text/javascript\">
function likeCounter(choice) {
var site = $(\"#site\").val();
var imgid = $(\"#imgid\").val();
$.get(\"./scripts/likecounter.php\", {_choice : choice, _site : site, _id : imgid},
function(returned_data) {
$(\"#choice\").html(returned_data);
}
);
}
</script>
//Changed to try and work with buttons
<button id=\"like\" onClick=\"likeCounter(this.id)\">Like</button>
<button id=\"dislike\" onClick=\"likeCounter(this.id)\">Dislike</button>
<input id=\"site\" type=\"hidden\" value=\"<?php echo $site; ?>\">
<input id=\"imgid\" type=\"hidden\" value=\"<?php echo $imgid; ?>\">

<br />
Likes: <span id=\"choice\"></span>


likecounter.php



<?php
include \'dbconnect.php\';

$site = $_GET[\'_site\'];
$imgid = $_GET[\'_id\'];
$input = $_GET[\'_choice\'];

if ($site == \"artwork\") {
$table = \"comics\";
}
else {
$table = \"artwork\";
}

$likes = $mysqli->query(\"SELECT like FROM $table WHERE id = $imgid\");
$dislikes = $mysqli->query(\"SELECT dislike FROM $table WHERE id = $imgid\");


if ($input == \"like\") {
$sql = \"UPDATE $table SET like = like + 1 WHERE id = $imgid\";
$mysqli->query($sql);
$likes++;

else if ($input == \"dislike\") {
$sql = \"UPDATE $table SET like = dislike + 1 WHERE id = $imgid\";
$mysqli->query($sql);
$dislikes++;
}
mysqli_close($mysqli);

echo \"Likes: \" . $likes . \", Dislikes: \" . $dislikes;

?>


It\’s not incrementing the database value, and it\’s not reporting out a value to the browser. Any suggestions?


Thanks!






Rating: 2 out of 5 based on 3 ratings



The post javascript – Implementing a like/dislike counter with php and jquery appeared first on Javascript ASK.






via Javascript ASK http://javascriptask.phpfogapp.com/javascript-implementing-a-likedislike-counter-with-php-and-jquery.html

No comments:

Post a Comment