duanpaxin3531 2019-04-08 11:13
浏览 106
已采纳

wordpress PHP与Ajax调用

Im looking to return user data back as a string through ajax with wordpress

Iv got the basic concept down

PHP

this is placed in my functions.php

add_action('template_redirect', 'edit_user_concept');
function edit_user_concept(){
      $id = $_POST['getbyID'];
      $user_info = get_userdata($id);
      echo 'Username: ' . $user_info->user_login . "
";
      echo 'User roles: ' . implode(', ', $user_info->roles) . "
";
      echo 'User ID: ' . $user_info->ID . "
";
}

Jquery/JS

    $.ajax({
         url: "http://www.example.com/",
         method: "POST",
         data: {
             getbyID: "23",
         },
         success: function(response){
             console.log(response);
             //example script logic here
         }
    })

the console log result is correct how ever its also logging lots of html elements that are not included in this. Such as

im not exactly sure why.

here is a small example to large to post in its full

Username: someusername
User roles: subscriber
User ID: 23
<!DOCTYPE html>
<html lang="en-US">
<div id="loadtheme" class="loadtheme"></div>
<head>

etc etc etc....

Any thoughts?

</div>
  • 写回答

2条回答 默认 最新

  • duan35557593 2019-04-08 11:26
    关注

    You are echoing the html without stopping it anywhere, you can use json response here, for json, first of all you need to use dataType: "json" in your ajax request as:

     url: "http://www.example.com/",
     method: "POST",
     dataType: "json" // need to use datatype here.
     data: {
         getbyID: "23",
     }
    

    Now, you need to store data in an array in your PHP as:

    $result = array();
    $result['Username'] = $user_info->user_login;
    $result['User_roles'] = implode(', ', $user_info->roles);
    $result['User_id'] = $user_info->ID;
    

    Now, you can use json_encode():

    echo json_encode($result);
    

    You can print json result as like:

    console.log(response.Username); // will print username
    console.log(response.User_roles); // will print user roles
    console.log(response.User_id); // will print user id
    

    Complete Example:

    $.ajax({
         url: "http://www.example.com/",
         method: "POST",
         dataType: "json" // json data type
         data: {
             getbyID: "23",
         },
         success: function(response){
             console.log(response.Username);
             console.log(response.User_roles);
             console.log(response.User_id);
             //example script logic here
         }
     });
    

    PHP:

    <?php
    function edit_user_concept(){
      $id = $_POST['getbyID'];
      $user_info = get_userdata($id);
      $result = array();
      $result['Username'] = $user_info->user_login;
      $result['User_roles'] = implode(', ', $user_info->roles);
      $result['User_id'] = $user_info->ID;
      echo json_encode($result); // will return json response
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?