I write messages into my db table with jQuery Ajax form post like this:
functions.php
add_action('wp_ajax_send_projectmessage', 'send_projectmessage');
function send_projectmessage() {
global $wpdb;
$wpdb->insert( //insert stuff to db table));
wp_send_json_success();
}
JS:
$('#form-pm').on('submit',function(e) {
var mydata = {
'action': 'send_projectmessage',
'projectid': projectid,
'message': message
};
var ajaxRequest =
$.ajax({
url: admin_ajax.ajax_url,
type: 'post',
data: mydata
});
ajaxRequest.done(function(data) { console.log(data); });
ajaxRequest.fail(function(jqXHR) { alert(jqXHR); });
e.preventDefault();
});
Everything works fine, data gets inserted into my database table.
Now, this is how I display the messages:
<div id="messages">
<?php
$messages = load_projectmessages($projectid);
foreach ($messages as $message) {
//make a <table><tr><td> with $message->themessage
}
?>
</div>
<form id="form-pm>
<!-- input fields and submit here -->
</form>
load_projectmessages
is in the same functions.php:
function load_projectmessages($projectid) {
global $wpdb;
$sql_displaymessages = $wpdb->prepare("sql stuff", $projectid );
$projectmessages = $wpdb->get_results($sql_displaymessages);
return $projectmessages;
wp_die();
}
My question is, where/when do I call load_projectmessages
? Should I modify my whole JS ajax code to also do the displaying of new data?
0