I am building a simple like system for my website. The problem is that, the jquery post is not working with what it supposed to do.
There is no values inserted on the database tables and the success function is not also working..
All database tables are already in set.
HTML:
<a href="javascript:like('85','17','product','1','3');" class="button like_click"><span><img src="wp-content/plugins/assets/images/icons/like-icon.png"> Like </span></a>
Here is my jQuery.
function like(blog_id,object_id,object_type,user_id,default_count)
{
jQuery.ajax({
url: '/wp-content/plugins/assets/like.php',
type: 'post',
data: 'object_id=' + object_id + '&user_id=' + user_id + '&type=like&blog_id=' + blog_id + '&object_type=' + object_type,
dataType: json,
success: function(data)
{
jQuery('#' + object_id + '_count').html(data.total);
jQuery('.like_click').attr('href','javascript:unlike(\'' + blog_id + '\',\'' + object_id + '\',\'' + user_id + '\',\'' + object_type + '\',\'' + default_count + '\')');
jQuery('.like_click span').html('<img src="/wp-content/plugins/assets/images/icons/unlike-icon.png"> Unlike');
jQuery('.likes').html('You and <a href="#">' + default_count + ' others</a> like this.');
}
});
}
The like.php
<?php
// include wordpress functions
include( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-admin/includes/plugin.php' );
global $wpdb;
$global_likes = $wpdb->base_prefix . "global_likes";
$global_table = $wpdb->base_prefix . "global_products_table";
$object_id = mysql_real_escape_string($_POST['object_id']);
$user_id = mysql_real_escape_string($_POST['user_id']);
$blog_id = mysql_real_escape_string($_POST['blog_id']);
$object_type = mysql_real_escape_string($_POST['object_type']);
$type = mysql_real_escape_string($_POST['type']);
$check_duplicate = $wpdb->get_row("SELECT object_id FROM ".$global_likes." WHERE object_type = '" .$object_type . "' and object_id = '".$object_id."' and blog_id = '" . $blog_id . "' and user_id = '" . $user_id . "' ");
if( empty($check_duplicate->object_id) && isset($_POST['object_id']) && isset($_POST['user_id']) && isset($_POST['blog_id']) && isset($_POST['object_type']) ) {
if( $type == "like" ) {
$wpdb->insert(
$global_likes,
array(
'user_id' => $user_id,
'blog_id' => $blog_id,
'object_id' => $object_id,
'object_type' => $object_type
)
);
$add_like = "UPDATE " . $global_table . " SET likes=likes+1 WHERE products_id = '".$object_id."' and blog_id = '" . $blog_id . "'";
$wpdb->query($add_like);
} elseif( $type == "unlike" ) {
$remove_like = "UPDATE " . $global_table . " SET likes=likes-1 WHERE products_id = '".$object_id."' and blog_id = '" . $blog_id . "'";
$delete_user_like = "DELETE FROM ".$global_likes." WHERE object_type = '" .$object_type . "' and object_id = '".$object_id."' and blog_id = '" . $blog_id . "' and user_id = '" . $user_id . "'";
$wpdb->query($remove_like);
$wpdb->query($delete_user_like);
}
$sql = mysql_fetch_assoc(mysql_query("SELECT likes FROM ".$global_table." WHERE products_id = '".$object_id."' and blog_id = '" . $blog_id . "'"));
echo json_encode(array('total' => $sql[0]['likes']));
} else {
wp_die('Error!');
}
?>
Is it also possible to just post without returning any value so I can remove the below script on like.php.
$sql = mysql_fetch_assoc(mysql_query("SELECT likes FROM ".$global_table." WHERE products_id = '".$object_id."' and blog_id = '" . $blog_id . "'"));
echo json_encode(array('total' => $sql[0]['likes']));
The reason is that, I already have a function that automatically get the like counts.
Please help.
You data in your ajax method is not json yet you specify the type as json.
Edit following comments: dataType is for the expected return data but the data: object should be in key value pair notation like the example below…
Ok, as a wise man once said RTM. Turns out that either method works.
From the Jquery documentation…
Which means, despite the upvotes, this answer wont help solve your problem at all, it’s just a syntactic alternative.