Why this code is not working?

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

  1. #1
    HI,

    Please see this code. Its not returning any value.

    Java Script

    <script type="text/javascript"> 
    			var url = "AjaxCall.php"; // The server-side script
    			 var xmlhttp;
    			function handleHttpResponse() {   
    	        if (xmlhttp.readyState == 4) {
    				var results=xmlhttp.responseText;
    				alert(results);
    				if(xmlhttp.status==200) {
                      var results=xmlhttp.responseText;
    				  alert(5);
    				  alert(results);
    				}
    			  }
    		    } 
    			function requestCustomerInfo() {     
    				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):

    HTMl form


    <form name="comment_form" action="">
    					<table  width="100%">
    						<tr>
    							<td>
    								Add your comments
    							</td>
    						</tr>
    						<tr>
    							<td>
    								<textarea name="comments" cols="40" rows="5"></textarea>
    							</td>
    						</tr>
    						<tr>
    							<td>
    								<input type="submit" value="Add Comment" onclick="requestCustomerInfo()">
    							</td>
    						</tr>
    					</table>
    					<input type="hidden" name="op" value="comment">
    				</form>
    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 12, 2007 IP
  2. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    var url = "AjaxCall.php";

    That's not a fully qualified url?

    Other than that, can you give more clues about what's not working?
     
    ecentricNick, Jul 12, 2007 IP
  3. kks_krishna

    kks_krishna Active Member

    Messages:
    1,495
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    80
    #3
    I want to display the values from the AjaxCall.php. Its not retyrning any values from that file
     
    kks_krishna, Jul 12, 2007 IP
  4. kks_krishna

    kks_krishna Active Member

    Messages:
    1,495
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    80
    #4
    Please help me.
     
    kks_krishna, Jul 12, 2007 IP
  5. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I'm not a java expert but I believe that if the javascript is being run client side, then the url should be fully qualified ie. instead of:

    var url = "AjaxCall.php";

    you need

    var url = "http://www.mywebsite.com/AjaxCall.php";
     
    ecentricNick, Jul 12, 2007 IP
  6. SEV3N

    SEV3N Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    When using Ajax you may use both relative and absolute paths to the .php file. In your case the AjaxCall.php must be in the same directory with the .js Ajax file.
     
    SEV3N, Jul 12, 2007 IP
  7. kks_krishna

    kks_krishna Active Member

    Messages:
    1,495
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    80
    #7
    Thanks. Its working fine for me. Thanks for your help.
     
    kks_krishna, Jul 12, 2007 IP