View Full Version : Php, html request not working

10-21-2012, 02:33 AM
I can not for the life of me get this code to work with my html file, why? This is saved as a before7.html file. The below is not everything in this code but it is very long so didn't think you would want all of it here.

.displaybox {
border:2px solid #000000;
font:24px normal verdana, helvetica, arial, sans-serif;
<script language="JavaScript" type="text/javascript">
function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (err2) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (err3) {
req = false;
return req;

var http = getXMLHTTPRequest();

function getServerTime() {
var myurl = 'telltimeXML.php';
myRand = parseInt(Math.random()*999999999999999);
var modurl = myurl+"?rand="+myRand;
http.open("GET", modurl, true);
http.onreadystatechange = useHttpResponse;

function useHttpResponse() {
if (http.readyState == 4) {
if(http.status == 200) {
var timeValue = http.responseXML.getElementsByTagName("timenow")[0];
document.getElementById('showtime').innerHTML = timeValue.childNodes[0].nodeValue;
} else {
document.getElementById('showtime').innerHTML = '<img src="anim.gif">';
<body style="background-color:plum" onLoad="getServerTime()">
<h1>Ajax Demonstration</h1>
<h2>Getting the server time without page refresh</h2>
<input type="button" value="Get Server Time" onClick="getServerTime()">
<div id="showtime" class="displaybox"></div>

And I have this php code saved as clock.php

header('Content-Type: text/xml');
echo "<?xml version=\"1.0\" ?><clock1><timenow>".date('H:i:s')."</timenow></clock1>";

10-21-2012, 04:44 AM
What is a before 7.html?

This in red
header('Content-Type: text/xml');
echo "<?xml version=\"1.0\" ?><clock1><timenow>".date('H:i:s')."</timenow></clock1>";
Turns the page into xml and I get errors when run. Remove it and the php echos the so called time.

10-21-2012, 12:10 PM
It may be that you have php short tags turned on in your php.ini file.

With php short tags, you open php code with <?php OR <?

When you then use xml in your php, <?xml contains <? doesn't it. Now, it shouldn't make any difference when being echoed as a string but I found php could be a bit screwy with this issue (such as reading an xml template from a file into a string using file_get_contents(() so you might want to look into turning off short tags and see if that works.