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 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机