I need to make a communicator using AJAX and XML file. I've already done function to download the data from the form and send it via AJAX to the server(XML file). The code below is in JavaScript.
function send(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 20) {
download(this);
}
xhttp.open("GET", "file.xml", true);
xhttp.send();
}
But I don't know how to make function that is sending an AJAX request to the server to download new messages and displaying them in the communicator.
function download(xml){
var xmlDoc = xml.responseXML;
var x = xmlDoc.getElementsByTagName("author");
}
I am downloading the data from the input in HTML and this is the way I save it to the XML file.
<?php
$author = $_GET["author"];
$message = $_GET["message"];
$file = 'file.xml';
if (!empty($author) && !empty($message)) {
$xml = simplexml_load_file($file);
$entry = $xml->addChild("entry");
$sxe = simplexml_import_dom($xml);
$sxe ->addChild("author", $author);
$sxe ->addChild("message", $message);
$xml->asXml($file);
}
header('location: index.html');
?>
My XML file looks like this after the send function.
<?xml version="1.0"?>
<entry>
<author>gnow</author>
<message>BAKDA</message>
<entry/>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…