I’m trying to develop a “like” system for my site. I’m using the code below:
like.php:
<?php
include_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');
global $wpdb;
$global_table = $wpdb->base_prefix . "global_products_table";
$blogid = $_GET['blog_id'];
$productsid = $_GET['products_id'];
mysql_query("UPDATE ".$global_table." SET likes=likes+1 WHERE blog_id=".$blogid." and products_id=".$productsid) or die(mysql_error());
$result = mysql_query("SELECT likes from ".$global_table." WHERE blog_id=".$blogid." and products_id=".$productsid) or die(mysql_error());
$row = mysql_fetch_assoc($result);
echo $row['likes'];
?>
index.html:
<form name="like_form">
<input type="hidden" name="products_id" value="<?php echo $product_info['products_id']; ?>" />
<a class="like_click"><span class="button"><img src="http://cdn1.iconfinder.com/data/icons/bijou/10/Like.png"> Like (<span class="like_count"><?php echo $product_info['likes']; ?></span>)</span></a>
</form>
JQuery:
<script type="text/javascript">
var $s = jQuery.noConflict();
$s(document).ready(function() {
$s('.like_click').click(function() {
var form = $s(this).closest('form[name=like_form]');
var blog_id = $s(form).find('input[name=blog_id]').val();
var products_id = $s(form).find('input[name=products_id]').val();
$s.post("<?php echo get_bloginfo('wpurl'); ?>/wp-content/plugins/wp-online-store/like.php?blog_id=<?php echo get_current_blog_id(); ?>&products_id='" + products_id + ", function(data) {
$s(form).find('span.like_count').html(data);
});
});
</script>
Problem: When I click the “like” link, it’s not working. The like count is not updating. When I type in the address bar the Url, e.g. http://website.com/wp-content/plugins/wp-online-store/like.php?blog_id=16&products_id=37, it does work. This makes me think the problem is with the JQuery. Also, I’m using WordPress.
try this first