Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery Like/Unlike issue
    primarykey
    data
    text
    <p>I have developed a simple like/unlike script. </p> <p><strong>Like Script</strong></p> <pre><code>function like(blog_id,object_id,object_type,user_id,default_count) { if( user_id == 0 ) { jQuery("#show_login_box").fancybox({cyclic: true}).trigger('click'); } else { if( default_count == 0 ) { var new_count = '1'; var link = 'unlike(\'' + blog_id + '\',\'' + object_id + '\',\'' + object_type + '\',\'' + user_id + '\',\'' + new_count + '\')\;'; jQuery('.likes').html('You like this.'); } else { var new_count = parseInt(default_count) + 1; if ( jQuery('.like-user').length ) { var name = jQuery('.like-user').html(); var link = 'unlike(\'' + blog_id + '\',\'' + object_id + '\',\'' + object_type + '\',\'' + user_id + '\',\'' + new_count + '\')\;'; jQuery('.likes').html('You and &lt;span class="like-user"&gt;' + name + '&lt;/span&gt; likes this.'); } else { var link = 'unlike(\'' + blog_id + '\',\'' + object_id + '\',\'' + object_type + '\',\'' + user_id + '\',\'' + new_count + '\')\;'; jQuery('.likes').html('You and &lt;span class="like-user"&gt;&lt;a href="#"&gt;' + default_count + ' others&lt;/a&gt;&lt;/span&gt; like this.'); } } jQuery('.like_click').attr('onclick',link); jQuery('.like_click span').html('&lt;img src="/wp-content/plugins/assets/images/icons/unlike-icon.png"&gt; Unlike'); 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, count: default_count }, dataType: 'json', success: function(data) { // jQuery('#' + object_id + '_count').html(data.total); } }); } } </code></pre> <p><strong>Unlike Script</strong></p> <pre><code>function unlike(blog_id,object_id,object_type,user_id,default_count) { if( default_count == 1 ) { var not_like = '0'; var link = 'like(\'' + blog_id + '\',\'' + object_id + '\',\'' + object_type + '\',\'' + user_id + '\',\'' + default_count + '\',\'' + not_like + '\')\;'; jQuery('.likes').html(''); } else { var new_count = parseInt(default_count) - 1; if ( jQuery('.like-user').length &amp;&amp; default_count &gt; 1 ) { var name = jQuery('.like-user').html(); var link = 'like(\'' + blog_id + '\',\'' + object_id + '\',\'' + object_type + '\',\'' + user_id + '\',\'' + new_count + '\')\;'; jQuery('.likes').html('&lt;span class="like-user"&gt;' + name + '&lt;/span&gt; like this.'); } else { var link = 'like(\'' + blog_id + '\',\'' + object_id + '\',\'' + object_type + '\',\'' + user_id + '\',\'' + new_count + '\')\;'; jQuery('.likes').html('&lt;span class="like-user"&gt;&lt;a href="#"&gt;' + new_count + ' people&lt;/a&gt;&lt;/span&gt; like this.'); } } jQuery('.like_click').attr('onclick',link); jQuery('.like_click span').html('&lt;img src="/wp-content/plugins/assets/images/icons/like-icon.png"&gt; Like'); jQuery.ajax({ url: '/wp-content/plugins/assets/like.php', type: 'POST', data: { object_id: object_id, user_id: user_id, type: 'unlike', blog_id: blog_id, object_type: object_type, count: default_count }, dataType: 'json', success: function(data) { // jQuery('#' + object_id + '_count').html(data.total); } }); } </code></pre> <p><strong>HTML</strong></p> <pre><code>&lt;a onclick="like('85','1','product','1','0');" class="button like_click"&gt; &lt;span&gt;&lt;img src="wp-content/plugins/assets/images/icons/like-icon.png"&gt; Like&lt;/span&gt; &lt;/a&gt;&lt;div class="likes"&gt;&lt;/div&gt; </code></pre> <p><strong>Scenario:</strong> I liked a post and im the first one so the message that returned is "You like this." and without refreshing I unliked it and it now removes the "You like this" message. Which is the correct response.</p> <p><strong>The Problem:</strong> Without refreshing the same post, I liked it again and now the message appears "You and 1 others like this" instead of "You like this." Message.</p> <p><strong>JS Fiddle Demo:</strong> <a href="http://jsfiddle.net/z8mKX/2/" rel="nofollow">http://jsfiddle.net/z8mKX/2/</a></p> <p>I can't configure what's wrong with my script. Please help</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload