Looks like you’re trying to use the http object’s response, but it didn’t perform the AJAX request
When you’re using jQuery’s AJAX methods, their success, error and complete callbacks will receive arguments when called. In the success callback, the first argument is the returned data from the server.
So, just a tiny change to your script and it should work
success: function(data) { //make sure data is specified as an argument
document.getElementById('insert_response').innerHTML = '<div class="success">Response : ' + data + '</div>'; //then you can use it here
}
Of course we now still need to know how to pass along that array of users.
Here I’m just passing an object in to the data part rather than the query string, since objects are a lot easier to play with.
(I’ve removed some of your code from the insert() function just to make this example a bit shorter)
function insert() {
// Just a static object at the moment, put your own values in here
var data = {
insert_what: "assign_user",
num_of_users: 4,
users: [
{ id: 1, name: "John" },
{ id: 2, name: "Steve" },
{ id: 3, name: "Tom" },
{ id: 4, name: "Harry" }
]
};
$.ajax({
type: "post",
url: "../ajax/insert.php",
dataType: "json", //set the datatype to JSON
data: data,
error: function(){
alert('Error loading document');
return false;
},
success: function(data) {
console.log(data); //check out what's in the console now.
document.getElementById('insert_response').innerHTML = '<div class="success">Response : '+data.message+'</div>';
}
});
}
I also made a change to the PHP file:
$num_of_users =
$_POST['num_of_users'];$users = $_POST["users"];
$returnData = array(
"insertData" => array(
"users" => $users,
"num_of_users" => $num_of_users
),
"message" => "Changes have been saved!");
header("Content-type: text/json");
print json_encode($returnData);
So here we grab some data from $_POST and stuff some relevant values (maybe extra return data too?) in to an associative array.
Then we send a json_encoded version of our $returnData back, jQuery will know it’s JSON because of the mime-type and convert this string in to a JavaScript object for us.
Hope this has clarified a few things