Help needed on AJAX with PHP

Discussion in 'PHP' started by kks_krishna, Jul 10, 2007.

  1. #1
    What is wrong with this code: I am getting any return values from PHP
    Html code

    <script type="text/javascript"> 
    			var url = "AjaxCall.php"; // The server-side script
    			 var xmlhttp;
    			function handleHttpResponse() {   
    				alert(6);
    	        if (xmlhttp.readyState == 4) {
    				alert(7);
    				var results=xmlhttp.responseText;
    				alert(results);
    				if(xmlhttp.status==200) {
                      var results=xmlhttp.responseText;
    				  alert(5);
    				  alert(results);
    				}
    			  }
    		    } 
    			function requestCustomerInfo() {     
    				//var sId = document.getElementById("txtCustomerId").value;
    				xmlhttp = getHTTPObject();
    				xmlhttp.open("GET", url, true);
    				xmlhttp.onreadystatechange = handleHttpResponse;
    				xmlhttp.send(null);
    			}
    			function getHTTPObject() {
    			     
    				  if(window.XMLHttpRequest){
    					xmlhttp = new XMLHttpRequest();
    				  }
    				  else if (window.ActiveXObject){
    					xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    					if (!xmlhttp){
    						xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    					}
       				}
    				return xmlhttp;
    			} 
    		</script>
    Code (markup):
    AjaxCall.php

    <?php
    
    echo "<table border='1'>
    <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
    <th>Hometown</th>
    <th>Job</th>
    </tr>";
     echo "<tr>";
     echo "<td></td>";
     echo "</tr>";
    echo "</table>";
    ?>
    Code (markup):
     
    kks_krishna, Jul 10, 2007 IP
  2. Greg Carnegie

    Greg Carnegie Peon

    Messages:
    385
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I am not an expert on this, but don't you need to execute your AJAX functions first?
     
    Greg Carnegie, Jul 10, 2007 IP
  3. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Your ajax code and initialization could be better.

    I rewrote it:

    function ajax()
    {
    	var url = "AjaxCall.php"; // The server-side script
    	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;
    			}
    		}
    	}
    	xmlHttp.onreadystatechange=function()
    	{
    		if(xmlHttp.readyState==4)
    		{
    			alert(xmlHttp.responseText);
    		}
    	}
    	xmlHttp.open("GET",url,true);
    	xmlHttp.send(null);
    }
    PHP:
    However just for your info you shouldn't use AJAX if all you want is a table.
    This makes more sense:

    
    alert("<table border='1'><tr><th>Firstname</th><th>Lastname</th><th>Age</th><th>Hometown</th><th>Job</th></tr><tr><td></td></tr></table>")
    
    PHP:
     
    MMJ, Jul 11, 2007 IP
  4. saurabhj

    saurabhj Banned

    Messages:
    3,459
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Good One :)
     
    saurabhj, Jul 11, 2007 IP