网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

wordpress PHP与Ajax调用

GG网络技术分享 2025-03-18 16:12 4


问题描述:

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>

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

我希望通过带有wordpress的ajax将用户数据作为字符串返回</ p>

Iv得到了基本概念</ p>

PHP </ strong> </ p>

这是放在我的functions.php中

</ p>

  add_action(\'template_redirect\',\'edit_user_concept\');  

function edit_user_concept(){

$ id = $ _POST [\'getbyID\'];

$ user_info = get_userdata($ id);

echo\'Username:\'。 $ user_info-&gt; user_login。 “

”;

echo\'用户角色:\'。 implode(\',\',$ user_info-&gt; roles)。 “

”;

echo\'用户ID:\'。 $ user_info-&gt; ID。 “

” 个; \\ N} </代码> </ PRE>

</ DIV>

</ DIV>

</ BLOCKQUOTE>

Jquery / JS </ strong> </ p>

</ p>

  $ .ajax({ 

url:“http://www.example.com/\",

method:”POST“,

data:{

getbyID:”23 “,

},

成功:函数(响应){

console.log(响应);

//这里的示例脚本逻辑

}

} )</ code> </ pre>

</ div>

</ div>

控制台日志结果是正确的,它如何记录大量的html 未包含在其中的元素。

</ p>

我不完全确定原因。</ p>

这里有一个小例子来发布 完整</ p>

</ p>

 用户名:someusername  

用户角色:subscriber

用户ID:23

&lt; !DOCTYPE html&gt;

&lt; html lang =“en-US”&gt;

&lt; div id =“loadtheme”class =“loadtheme”&gt;&lt; / div&gt;

&lt; head&gt; \\ r

etc等等.... </ code> </ pre>

</ div>

</ div>

任何想法 ?</ p>

</ div>

网友观点:

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

}

?>

###

Try exit or return after echo.I dont know the complete problem but hope this helps

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 . \\\"

\\\".\'User roles: \' . implode(\', \', $user_info->roles) . \\\"

\\\".\'User ID: \' . $user_info->ID . \\\"

\\\";

exit;

}

标签:

提交需求或反馈

Demand feedback