This time I will bring you a detailed step-by-step explanation of the direct implementation of the like function with Ajax. What are the precautions for Ajax to directly implement the like function. The following is a practical case, let's take a look.
The first step is to remove the redundant one in the module --->News Mood--->Mood Configuration in the background, change the name to "Like", submit and save.
The second is the template file to modify the news mood:/phpcms/templates/default/mood/index.html
Delete all codes and only return the total number of likes
{loop $setting $k $v} {$data[$v['fields']]} {/loop}
Template The implementation of the page is as follows. Processed by looping array
<p class="info"> <a href="http://phpcms.com/content-150-1-1.html" target="_blank" title="展会现场人头攒动"><h4 class="title ellipsis download">展会现场人头攒动</h4></a> <p class="date">2017-02-01</p> <p class="sum"> <span class="laud"><i class="iconfont zanbtn" >赞</i><a href="javascript:;" rel="external nofollow" <span style="min-width: 0px; min-height: 0px; box-sizing: border-box; color: rgb(34, 34, 34); unicode-bidi: -webkit-isolate; font-family: Consolas, "Lucida Console", monospace; white-space: pre-wrap;">id</span><span style="color: rgb(34, 34, 34); font-family: Consolas, "Lucida Console", monospace; white-space: pre-wrap;">="</span><span style="min-width: 0px; min-height: 0px; box-sizing: border-box; color: rgb(34, 34, 34); unicode-bidi: -webkit-isolate; font-family: Consolas, "Lucida Console", monospace; white-space: pre-wrap;">zans{$r[id]}</span><span style="color: rgb(34, 34, 34); font-family: Consolas, "Lucida Console", monospace; white-space: pre-wrap;">" </span> title="<span style="color: rgb(34, 34, 34); font-family: Consolas, "Lucida Console", monospace; white-space: pre-wrap;">$r[id]}</span>">0</a></span> </p> </p>
ajax. K=8 means likes.
$(".zanbtn").on("click",function(){ var id = $(this).next().attr("title"); var aval = $(this).next("a").html(); $.getJSON('http://phpcms.com/index.php?m=mood&c=index&a=post&id=150-'+id+'-1&k=8&'+Math.random()+'&callback=?', function(data){ if(data.status==1) { console.log( $(this).next()); $("#zans"+id).html(data.data); }else { alert(data.data); return false; } }) });
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related matters on the PHP Chinese website. article!
Recommended reading:
Detailed explanation of the steps of Ajax deleting data and viewing data operations
How to use ajax to implement pop-up windows Log in
The above is the detailed content of Detailed explanation of the steps to directly implement the like function with Ajax. For more information, please follow other related articles on the PHP Chinese website!