Modify your code as follows:
add_action('wp_ajax_send_projectmessage', 'send_projectmessage');
function send_projectmessage() {
global $wpdb;
$wpdb->insert( //insert stuff to db table));
// Call your function to retrieve your data and send it to browser
$messages = load_projectmessages($projectid);
wp_send_json_success($messages);
}
Display data through JS by modifying this
ajaxRequest.done(function(data) { console.log(data); });
to this
ajaxRequest.done(function(data) {
// Put markup for retrieved data here
// for example
var o = "<table><tr><td>Project ID</td><td>Message</td></tr>";
for (i=0; i<=data.length; i++){
o .= "<tr><td>"+data.projectid+"</td><td>"+data.message"+</td></tr>";
}
o .= "</table>";
$(#messages).html(o);
});
Also make sure that your function return an array instead of an OBJECT, so modify it as follows:
function load_projectmessages($projectid) {
global $wpdb;
$sql_displaymessages = $wpdb->prepare("sql stuff", $projectid );
// Add ARRAY_A to get result in an associative array
$projectmessages = $wpdb->get_results($sql_displaymessages, ARRAY_A);
return $projectmessages;
// Not needed
// wp_die();
}
I hope this will help.