Results 1 to 3 of 3

Thread: AJAX dynamic content through links

  1. #1
    Join Date
    Apr 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default AJAX dynamic content through links

    Okay, I have a page that lists the headline and subheadline of a story retrieved from a database in php. I am trying to get the story to be dynamically loaded when the user clicks on the headline. However, when the user clicks on the headline, nothing happens. At all. I am somewhat experienced with php but I am totally new to javascript. Backwards, I know. So needless to say, jumping right into AJAX with it is really confusing. So far, this is what I have. I would really appreciate any help you have to offer. Thanks in advance.

    PHP Code:
        $query "SELECT * FROM `May 2009` WHERE `category` = 'News' ORDER BY `id` DESC LIMIT 0 , 30";
        
    $resultmysql_query($query) or die(mysql_error()); 
                            
        while (
    $row mysql_fetch_row($result)){

            echo 
    "<div class=\"pad\"></div>\n";
            echo 
    "<div class=\"post\">\n";
            echo 
    "<h1 class=\"title\" onclick=\"showStory(" .$row[5]. "," .$row[0]. ")\">" $row[3] . "</a></h1>\n";
            echo 
    "<h3 class=\"title\"><a href=\"#\">" $row[4] . "</a></h3>\n";
            echo 
    "<div class=\"entry\" name=\"" .$row[0]. "\">";
            echo 
    "</div>";
            echo 
    "</div>";
        }
    ?> 
    Code:
    <script type="application/javascript"> 	
    	
    	var xmlhttp, story, id;
    
    	function showStory(story, id){
    
    	xmlhttp=GetXmlHttpObject();
    	if (xmlhttp==null){
      		alert ("Your browser does not support XMLHTTP!");
      	return;
      	}
    
            xmlhttp.onreadystatechange=stateChanged();
    	}
    
    	function stateChanged(){
    	
    		if (xmlhttp.readyState==4){
      		document.getElementsByName(id).innerHTML=story;
      		}
    	}
    
    	function GetXmlHttpObject(){
    	if (window.XMLHttpRequest){
      	// code for IE7+, Firefox, Chrome, Opera, Safari
      	return new XMLHttpRequest();
      	}
    	if (window.ActiveXObject){
      	// code for IE6, IE5
      	return new ActiveXObject("Microsoft.XMLHTTP");
      	}
    	return null;
    	}
    </script>
    Last edited by SMartin91; 06-20-2009 at 07:33 AM.

  2. #2
    Join Date
    Jul 2008
    Posts
    128
    Thanks
    0
    Thanked 17 Times in 16 Posts

    Default

    Code:
    	var xmlhttp, story, id;
    
    	function showStory(story, id){
    
    	xmlhttp=GetXmlHttpObject();
    	if (xmlhttp==null){
      		alert ("Your browser does not support XMLHTTP!");
      	return;
      	}
    
            xmlhttp.onreadystatechange=stateChanged();
    	}
    
    	function stateChanged(){
    	
    		if (xmlhttp.readyState==4){
      		document.getElementsByName(id).innerHTML=story;
      		}
    	}
    The id parameter passed to showStory isn't made visible to stateChanged. This must be generating a console error.

  3. #3
    Join Date
    Apr 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ah, I see. I did make some advancement with that help but instead, I opted for a pre-coded solution as this is only for a personal site. Thank you though, it was much appreciated :]

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
  •