Ajax with PHP
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Ajax with PHP

  1. #1
    Join Date
    Aug 2005
    Posts
    69

    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:
    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;
    			}
    		}
    
    		xmlHttp.open("GET", "http://localhost/time.php", true);
    		xmlHttp.send(null);
    	}
    	</script>
    </head>
    
    <body>
    
    <form name="myForm">
    	Name: <input type="text" onkeyup="ajaxFunction();" name="username" />
    	Time: <input type="text" name="time" />
    </form>
    
    </body>
    </html>
    Server Side PHP Script:
    Code:
    <?php
    	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.
    ~Donotalo()

  2. #2
    Join Date
    Jul 2007
    Location
    Sweden
    Posts
    331

    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
    Posts
    10,886

    Re: Ajax with PHP

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

    Code:
    xmlHttp.open("GET", "time.php", true);
    xmlHttp.open("GET", "../time.php", true);
    xmlHttp.open("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

This is a CodeGuru survey question.


Featured


HTML5 Development Center