I spent some time last night looking at implementing AJAX for posting starting with the journal. I've got the posting part working okay using the jQuery $.post
method but am having problems refreshing the page content with the new value once submitted to the database.
16/06/2021
2021/06/16#p2
On Monday, the UK Government announced a delay to lifting the final Covid restrictions to 19th July. A wise move in my opinion. Today, however, parliament gets to debate and vote on that delay which could (although it's highly unlikely) overturn the decision. Some rebel Conservative MPs have said they will vote against the Government but I doubt it will be in sufficient numbers.
The announcement was made as though a fait accompli, just as some businesses and individuals viewed the original "Freedom Day" of June 21st, as it had been dubbed, and made plans accordingly only to complain when those plans were made redundant.
The media has hyped the original date leading many to assume it was set in stone despite always being the earliest the change could happen, a "not before" marker subject to change. I suppose it illustrates the dangers of making assumptions and choosing what you want to hear rather than the truth.
2021/06/16#p3
After a bit of research and trial and error (more error than trial I have to say) I now have a version of the journal that automatically updates the page contents after posting. The trick appears to be creating a new XMLHttpRequest
to retrieve the result of a new query:
var XHR = new XMLHttpRequest();
XHR.open("GET", "updatejournal.php", true);
XHR.send();
updatejournal.php
contains both the code to perform the update (as called by $.post
) and the query to retrieve the updated entry, it builds the HTML before returning it to the journal page which replaces the innerHTML
content of the entry.
I'm not sure the same method would be overly practical for new posts but I may be able to tweak it for commenting.