Вы повторяете html, не останавливаясь нигде, вы можете использовать ответ json здесь, для json
, прежде всего вам нужно использовать dataType: "json"
в вашем запросе ajax
как:
url: "http://www.example.com/",
method: "POST",
dataType: "json" // need to use datatype here.
data: {
getbyID: "23",
}
Теперь вам нужно хранить данные в array
в вашем PHP
как:
$result = array();
$result['Username'] = $user_info->user_login;
$result['User_roles'] = implode(', ', $user_info->roles);
$result['User_id'] = $user_info->ID;
Теперь вы можете использовать json_encode()
:
echo json_encode($result);
Вы можете напечатать json
результат как:
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
Полный пример:
$.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
}
?>