On Sat, 1 Jul 2017 02:49:24 -0700 (PDT),
bit-n...@hotmail.com wrote:
> ....OK, so assume there's some text on the screen, like a headline, or topic, say. When it's clicked, the actual text has to be got (from the server), and shown.
> What would the coding look like?
> (assume the text itself is on a MySQL db on the server, with some kind of "id" to it, which will be fetched by a PHP script - ie.
www.site.com/gettext.php?articleid=210).
Use XMLHttpRequest. e.g. for that example URL...
function fetchArticle(articleID) {
var xhr = new XMLHttpRequest();
xhr.addEventListener("error", function() {
console.log("fetchArticle() failed with HTTP code " + xhr.status + ". " + xhr.statusText);
});
xhr.addEventListener("load", function() {
var eleArticleContainer = document.getElementById("articleContainer");
eleArticleContainer.textContent = xhr.responseText;
/*
Use below code if the article text is HTML formatted.
eleArticleContainer.innerHTML = xhr.responseText;
*/
});
xhr.open("GET", "
https://www.site.com/gettext.php?articleid=" + articleID);
xhr.send();
}
//usage e.g.
fetchArticle(250);