I'm trying to get some basic Javascript and PHP working for a coding challenge I'm taking part in and I need to receive a JSON object from a PHP file to use in my Javascript that displays the current date on a HTML page. I've tried a few different methods and can't seem to get any to work, is there something I'm missing here?
I've tried using jQuery getJSON as well as running the PHP in the same HTML file as the Javascript.
This is the PHP file and its output:
echo json_encode([
"date" => getthedate()
]);
function getthedate() {
return date("Y/m/d");
}
{"date":"2019\/07\/04"}
This is what I've most recently been trying in Javascript:
function getDate() {
var theDate = $.getJSON(
"http://localhost/coding-challenge/get-date.php",
function(data)
);
JSON.parse(theDate);
document.getElementsByClassName("date")[0].innerHTML = theDate;
}
I was expecting this Javascript to retrieve the JSON object that the PHP file echoes and then display it in the HTML file's "date" class, an empty paragraph. However this just results in a blank space where the date paragraph is.