CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3

Thread: Ajax with PHP

  1. #1
    Join Date
    Aug 2005

    Ajax with PHP

    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:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "">
    <html xmlns="" xml:lang="en">
    	<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;
 "GET", "http://localhost/time.php", true);
    <form name="myForm">
    	Name: <input type="text" onkeyup="ajaxFunction();" name="username" />
    	Time: <input type="text" name="time" />
    Server Side PHP Script:
    	print time();
    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.

  2. #2
    Join Date
    Jul 2007

    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.

  3. #3
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002

    Re: Ajax with PHP

    In other words...use relative paths (in relation to the HTML file).

    Code:"GET", "time.php", true);"GET", "../time.php", true);"GET", "folder/time.php", true);
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)