Home > Web Front-end > JS Tutorial > How to use AJAX to asynchronously obtain avatars of comment users in WordPress_javascript skills

How to use AJAX to asynchronously obtain avatars of comment users in WordPress_javascript skills

WBOY
Release: 2016-05-16 15:20:48
Original
1993 people have browsed it

After the commenter enters their email address, the commenter's avatar is asynchronously obtained and displayed. Personally, I feel that although this function cannot bring much improvement to the user experience, and it is not a practical function, it is at least very cool. See Some websites have added this function, so I also wrote a script because I didn't want to be lonely. I didn't have time to encapsulate it, so I went directly to the principle and code.

Principle of asynchronously dynamically calling avatars

  • Get user input
  • Filter user input
  • Pass variables to the background
  • Process data in the background and return the HTML code of the avatar
  • Get the background return data and load the HTML code into the current page

It seems like a lot of steps, but it is actually very simple. We only need to slightly modify our theme to achieve the effect.

Simple function screenshot:

201618171924137.jpg (519×388)

Achievement

Function code: JavaScript
The following code needs to be integrated into the JQuery framework.
The apiurl variable is your php api interface file address, which is found below the file code.
The function mainly focuses on the action of losing focus of the email input box.

 function getAvatar(authorEmail) {//获得头像代码封装函数
 var nowtime = Math.round(new Date().getTime() / 1000);
 $.get(apiurl, {
 action : "get_avatar",
 email : authorEmail,
 t : nowtime
 }, function(data) {
 $('#get-avatar-img').fadeOut('slow', function() {
 $('#get-avatar-img').html(data).fadeIn();
 })
 });
 }
 
 var avatarhtml = '<div id="get-avatar-img" style="display:none;">';
 avatarhtml += '</div>';
 $('#respond').append(avatarhtml);//添加头像HTML
 if($('#email').val().length > 1)
 getAvatar($('#email').val());//获得邮箱地址
 $('#email').focusout(function() {//email输入框失去焦点绑定的动作
 var authorEmail = $('#email').val();
 var pattern = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
 var flag = pattern.test(authorEmail);
 if(flag) {
 $('#get-avatar-img').html('载入头像中').fadeIn('fast');
 getAvatar(authorEmail);
 } else {
 alert('请输入正确邮箱地址');
 }
 })

Copy after login
Function code: PHP
Background response code, here I use a separate page file to respond,
The advantage of this is that you don’t have to call this part of the code every time you open the page,
Responding only when a request is made completely eliminates the theme's backwards compatibility concerns.
Of course, you can also mount the response function to the wp hook.
 $action = isset($_REQUEST['action']) &#63; $_REQUEST['action'] : false ;
 
if($action){//留下以后添加功能的空间,你懂的。
 switch ($action) {
 case 'get_avatar':
 $email = isset($_REQUEST['email']) &#63; $_REQUEST['email']: false ;
 if($email){
 echo get_avatar($email,60);
 }
 break;
 
 default:
 echo "请求内容不充分";
 break;
 }
 }
Copy after login

Summary
So……. Very simple, right?
Request-> Response-> Add A total of three steps.
Of course, in order to enhance logic and highlight organization, some necessary data are filtered here
There are also some misjudgments, let’s just leave these for thinking.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template