I'm trying to write the server side script in PHP. The following code is supposed to show the value returned by time() inside a text box. But it is not doing that. Where is the problem?
Client Side Ajax Script:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Ajax Practice</title>
<script type="text/javascript">
function GetXmlHttpObject() {
var xmlHttp;
try {
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e) {
// Internet Explorer
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
return xmlHttp;
}
function ajaxFunction() {
var xmlHttp = GetXmlHttpObject();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {// The request is complete
document.myForm.time.value = xmlHttp.responseText;
}
}
I'm using XAMPP and the file time.php is placed in C:\Program Files\XAMPP\xampp\htdocs. When I open http://localhost/time.php, it is showing correct result.
Please help.
July 13th, 2007, 02:48 AM
andreasblixt
Re: Ajax with PHP
There is nothing wrong with your code.
Your problem is that due to the standard security settings in browsers, requests using XmlHttpRequest are limited to paths within the context of the calling document. So the solution to your problem is to make sure the HTML document is on the same server as time.php.
July 13th, 2007, 08:46 AM
PeejAvery
Re: Ajax with PHP
In other words...use relative paths (in relation to the HTML file).