0

I am getting the error "This page contains the following errors: error on line 10 at column 13: XML declaration allowed only at the start of the document Below is a rendering of the page up to the first error".

I am trying to open the html file in the php file by using require(), I am also using a javascript file to run the program. I want to display data from my php database inside a table.

Maybe I should have the javascript and php in the same file? Any help to get this working would be greatly appreciated, thank you.

airportArrivals.html

<html>
<head>  
<script type="text/javascript"> </script>
</head>
<hr></hr>

<hr></hr>
<div id="myDiv"></div>
<a href="http://localhost/airport/airportArrivals.php">Arrivals</a> | <a href="http://localhost/airport/liveArrivalsJS.php">LiveArrivals(JS)</a> | <a href="http://localhost/airport/liveArrivalsNode.js">Live Arrivals(Node)</a> | <a href="http://localhost/airport/admin.php">Admin</a>
</html>

liveArrivalsJS.js

window.onload=function(){
    getAjaxData();
}

var xmlhttp;

function getAjaxData()
{   
    if (window.XMLHttpRequest)
    {
        // IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        // IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }   
    xmlhttp.onreadystatechange = showXMLData;       
    xmlhttp.open("GET", "http://localhost/airport/liveArrivalsJS.php", true);       
    xmlhttp.send(); 
}

function showXMLData()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)   {       

        var terminal = xmlhttp.responseXML.getElementsByTagName("terminal");
        var origin = xmlhttp.responseXML.getElementsByTagName("origin");
        var airline = xmlhttp.responseXML.getElementsByTagName("airline");
        var flight = xmlhttp.responseXML.getElementsByTagName("flight");
        var scheduledDate = xmlhttp.responseXML.getElementsByTagName("scheduledDate");
        var scheduledTime = xmlhttp.responseXML.getElementsByTagName("scheduledTime");
        var status = xmlhttp.responseXML.getElementsByTagName("status");            

        var output = '<table border = 1>';                     
        for (var i=0;i<terminal.length;i++) {
            output += '<tr><td>' + terminal[i].firstChild.nodeValue + '</td><td>' + origin[i].firstChild.nodeValue + '</td><td>' + airline[i].firstChild.nodeValue +'</td></tr>' + flight[i].firstChild.nodeValue + '</td><td>'+ scheduledDate[i].firstChild.nodeValue + '</td><td>'+ scheduledTime[i].firstChild.nodeValue + '</td><td>'+ status[i].firstChild.nodeValue + '</td><td>'
        }
        output += '</table>';
        document.getElementById("myDiv").innerHTML = output;            
    }
}

liveArrivalsJS.php

<?php
require 'airportArrivals.html';

$xmlDom = new DOMDocument();
$xmlDom->appendChild($xmlDom->createElement('results'));
$xmlRoot = $xmlDom->documentElement;

$connection = mysqli_connect("localhost","root","");
mysqli_select_db($connection,"airportdb");
$result = mysqli_query($connection,"select * from arrivals");

while($row=mysqli_fetch_array($result))
{
    $xmlRowElementNode = $xmlDom->createElement('row');
    $i=0;
    for($i=0;$i<mysqli_num_fields($result);$i++)
    {
        $xmlRowElement = $xmlDom->createElement($result->fetch_field_direct($i)->name);
        $xmlText = $xmlDom->createTextNode($row[$i]);
        $xmlRowElement->appendChild($xmlText);
        $xmlRowElementNode->appendChild($xmlRowElement);
    }
    $xmlRoot->appendChild($xmlRowElementNode);
}    
mysqli_close($connection);
header('Content-type:  text/xml');
print($xmlDom->saveXML());
?>
GleneaMan
  • 167
  • 1
  • 2
  • 13
  • You're attempting to parse XML with an XML declaration somewhere other than at the very top of the XML document. There cannot be a comment or even blank space before the XML declaration. See the duplicate link for the three possibilities to check. – kjhughes Dec 02 '16 at 21:20
  • Thanks a million kjhughes, I deleted "header('Content-type: text/xml');" from the button of the liveArrivalsJS.php file and it is retrieving the information for me. The information is all in one long line though but I want to display it in a table form. Can I 'require 'liveArrivalsJS.js' from the liveArrivalsJS.php file like I did with 'require 'airportArrivals.html'? Thanks. – GleneaMan Dec 03 '16 at 10:30
  • Please post completely new questions separately, not buried in comments. Thanks. – kjhughes Dec 03 '16 at 16:06

0 Answers0