Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: ajax with php

  1. #1
    New Coder
    Join Date
    Aug 2008
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question ajax with php

    I have a function using Ajax to get data and while it gets the data a loading image appears but its not appearing, something wrong with the <img> syntax:

    Code:
    print("
    		<script type=\"text/javascript\">
    		var http = false;
    
    		http = new XMLHttpRequest();
    
    		function validate(user) {
    		  http.abort();
    		  document.getElementById('test').innerHTML = \"<img src=\"images/load.gif\"/>\";
    		  http.open(\"GET\", \"tracks.php?name=\" + user, true);
    		  http.onreadystatechange=function() {
    			if(http.readyState == 4) {
    			  document.getElementById('test').innerHTML = http.responseText;
    			}
    		  }
    		  http.send(null);
    		}
    		</script>
    	");
    How do i fix that?

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Maybe it's not appearing because the AJAX call is being executed too fast? What happens if you do something like this:
    Code:
    var http = null;
    function validate(user) {
       http.abort();
       document.getElementById('test').innerHTML = '<img src="images/load.gif"/>';
       window.setTimeout(function() {request(user);}, 2000);
    }
    
    function request(user) {
       http.open("GET", "tracks.php?name=" + user, true);
       http.onreadystatechange=function() {
          if(http.readyState == 4) {
             document.getElementById('test').innerHTML = http.responseText;
          }
       }
       http.send(null);
    }
    EDIT: Ah, and even more important. By doing this
    Code:
    print("document.getElementById('test').innerHTML = \"<img src=\"images/load.gif\"/>\";");
    you will get this in javascript
    Code:
    document.getElementById('test').innerHTML = "<img src="images/load.gif"/>";
    which is a wrong use of quotes
    Last edited by devnull69; 01-03-2011 at 07:31 AM.


  •  

    Posting Permissions

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