SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Trinidad and Tobago
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    AJAX request in a loop

    is it possible to make an AJAX request within a loop. I am trying to request some info from my db via php for a known number of times but it doesn't seem to work. The function reaches as far as the alert(value) but nothing further. The code is shown below
    get_bookmarks.php
    PHP Code:
    <?php
     $id 
    $_REQUEST['id'];
     
    //$id=$_GET['id'];
     
    $con mysql_connect("localhost","root","");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }
    mysql_select_db("joomla"$con);

    //query databases for a result
    $result mysql_query("SELECT * FROM jos_content where id='$id'");

    while(
    $row mysql_fetch_array($result))
     {
        echo 
    "<li><a href=\"JavaScript:article('$id')\">".$row['title']."</a> </li>";
     }
    ?>
    bookmarks.html
    HTML Code:
    <html>
    	<head>
    		<title>List Bookmarks</title>
    	</head>
    	<body>
    		<script>
    		
    		window.onload = function() {
    		displaybookmarks();
    		};
    		
    		function article(Article) {
     
    		  HttPRequest = false;
    		  if (window.XMLHttpRequest) { // Mozilla, Safari,...
    			 HttPRequest = new XMLHttpRequest();
    			 if (HttPRequest.overrideMimeType) {
    				HttPRequest.overrideMimeType('text/html');
    			 }
    		  } else if (window.ActiveXObject) { // IE
    			 try {
    				HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
    			 } catch (e) {
    				try {
    				   HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
    				} catch (e) {}
    			 }
    		  } 
     		  if (!HttPRequest) {
    			 alert('Cannot create XMLHTTP instance');
    			 return false;
    		  }
     			var url = 'includes/article_desc.php';
    			var pmeters = '&Article='+Article;
    			HttPRequest.open('POST',url,true);
     
    			HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    			HttPRequest.setRequestHeader("Content-length", pmeters.length);
    			HttPRequest.setRequestHeader("Connection", "close");
    			HttPRequest.send(pmeters);
     
     
    			HttPRequest.onreadystatechange = function()
    			{
     			if(HttPRequest.readyState == 3)  // Loading Request
    				  {
    			document.getElementById("listarticles").innerHTML = '<img src="images/spiral.gif" align="center" />';
    				  }
     				 if(HttPRequest.readyState == 4) // Return Request
    				  {
    					var response = HttPRequest.responseText;
    					document.getElementById("listarticles").innerHTML = response;
    				  }
     			}
     			   }	
    			
    			
    			function displaybookmarks(){
    				for(var i=0;i<localStorage.length;i++)
    					{
    						var keyName = localStorage.key(i);
    						var value = localStorage.getItem(keyName);
    						alert(value);
    						function getbookmarks(id);
    					}
    			}
    		
    		function getbookmarks(id) {
     
    		  HttPRequest = false;
    		  if (window.XMLHttpRequest) { // Mozilla, Safari,...
    			 HttPRequest = new XMLHttpRequest();
    			 if (HttPRequest.overrideMimeType) {
    				HttPRequest.overrideMimeType('text/html');
    			 }
    		  } else if (window.ActiveXObject) { // IE
    			 try {
    				HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
    			 } catch (e) {
    				try {
    				   HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
    				} catch (e) {}
    			 }
    		  } 
     		  if (!HttPRequest) {
    			 alert('Cannot create XMLHTTP instance');
    			 return false;
    		  }
     			var url = 'includes/getbookmarks.php';
    			var pmeters = '&id='+value;
    			HttPRequest.open('POST',url,true);
     
    			HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    			HttPRequest.setRequestHeader("Content-length", pmeters.length);
    			HttPRequest.setRequestHeader("Connection", "close");
    			HttPRequest.send(pmeters);
     
     
    			HttPRequest.onreadystatechange = function()
    			{
     			if(HttPRequest.readyState == 3)  // Loading Request
    				  {
    			document.getElementById("listarticles").innerHTML = '<img src="images/spiral.gif" align="center" />';
    				  }
     				 if(HttPRequest.readyState == 4) // Return Request
    				  {
    					var response = HttPRequest.responseText;
    					document.getElementById("listarticles").innerHTML = response;
    				  }
     			}
     			   }	
    			
    						
    		
    		</script>
    	<div id="listarticles"></div>	
    	</body>
    </html>

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    for(var i=0;i<localStorage.length;i++)
    					{
    						var keyName = localStorage.key(i);
    						var value = localStorage.getItem(keyName);
    						alert(value);
    						function getbookmarks(id);
    					}
    			}
    Use of the function keyword there is a syntax error. USE THE ERROR CONSOLE.

    If you're going to intialise a series of asychronous requests in a loop (inadvisable), you cannot use a common request object as your code does.

    Use the readyState==4 block to initialise any subseqent requests.
    Tab-indentation is a crime against humanity.

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Trinidad and Tobago
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Logic Ali View Post
    Use of the function keyword there is a syntax error. USE THE ERROR CONSOLE.

    If you're going to intialise a series of asychronous requests in a loop (inadvisable), you cannot use a common request object as your code does.

    Use the readyState==4 block to initialise any subseqent requests.
    I got this to work by the way but not in the way you pointed out. Why is it not advisable to make asynchronous requests in a loop? lastly how can I use the readyState==4 to achieve this


Bookmarks

Posting Permissions

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