Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8759103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:43:02+00:00 2026-06-13T14:43:02+00:00

I am building a simple like system for my website. The problem is that,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:43:03+00:00Added an answer on June 13, 2026 at 2:43 pm

    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…

    data: { object_id: object_id, user_id: user_id, type: 'like', blog_id: blog_id, object_type: object_type }
    

    Ok, as a wise man once said RTM. Turns out that either method works.

    From the Jquery documentation…

    The data option can contain either a query string of the form
    key1=value1&key2=value2, or a map of the form {key1: ‘value1’, key2:
    ‘value2’}. If the latter form is used, the data is converted into a
    query string using jQuery.param() before it is sent. This processing
    can be circumvented by setting processData to false. The processing
    might be undesirable if you wish to send an XML object to the server;
    in this case, change the contentType option from
    application/x-www-form-urlencoded to a more appropriate MIME type.

    Which means, despite the upvotes, this answer wont help solve your problem at all, it’s just a syntactic alternative.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a quite simple content management system that does not require such an
I'm building a simple contact form for a website. It does not connect to
I'm working with some simple django-tastypie Resources with the following problem: Imagine I'm building
I'm building a simple system like a realtime news feed, using node.js + socket.io.
I am building a simple messaging system with Rails3 that allows users to send
I am building a simple event system and i am having problem with rails
I'm building a simple code-first MVC 3 blog like application. My model has three
I'd like to print a simple table in my page with 3 columns, building
I am building simple notificiation system and I just wanted to know what is
Im building a simple chat client which is only supposed to be able to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.