SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    AJAX problem, please help me...

    Hi,

    I would like to ask You for help. I'm trying to make the AJAX photo gallery(very simle), but I've problem with the catch response from php script...

    Here is php script code:
    PHP Code:
    <?php
    $which
    =$_GET["id"];
    $conn=mysql_connect('localhost''root''password'//in normal script I use real user and password...
    or die(mysql_error());
    mysql_select_db('db'$conn)
    or die(
    mysql_error());
    $query="SELECT*FROM images WHERE id='$which'";
    $r=mysql_query($query);
    $row=mysql_fetch_array($r);
    $path='/gallery/images/photos/'.$row[1];
    echo 
    "$path";
    ?>

    And here is JS code:
    Code:
    <script type="text/javascript">
    var http_request=false;
    
    function makeRequest(id){
    	http_request=false;
    	if(window.XMLHttpRequest){
    		http_request=new XMLHttpRequest();
    	}
    	else if(window.ActiveXObject){
    		try{
    			http_request=new ActiveXobject("Msxml2.XMLHTTP");
    		}
    		catch(e){
    			try{
    				http_request=new ActiveXObject("Microsoft.XMLHTTP");
    			}
    			catch(e){}
    	}
    }
    http_request.onreadystatechange=function(){switchImage(http_request);};
    http_request.open('GET','function.php?id='+id,true); // ***Probably the problem is here***
    http_request.send(null);
    }
    
    function switchImage(http_request){
    	var el=document.getElementById('pic');
    	while(el.firstChild){
    		el.removeChild(el.firstChild);
    	}
    	if(http_request.readyState==4){
    		if(http_request.status==200){
    			var pic=document.createElement("img");
    			pic.setAttribute("src","+http_request.responseText+"); // ***Or here***
    			el.appendChild(pic);
    		}
    	}
    	else{
    	var info=document.createTextNode("Loading...");
    	el.appendChild(info);
    	}
    }
    </script>
    In this script I put the coments where probably something is wrong. I've also try to use responseXML, but it didn't help.

    The php script works well couse I've tried it. You can see the whole script and
    and HTML here:

    Click

    The xhtml and css it's not finished, the thumbnails will be scrollable... But first I want to finished AJAX script.

    Thanks for all responses!!!

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doesn't look like you are initializing the http_request object anywhere.

    You should define the onreadystatechange event handler after you call the open method (but obviously before the send method).


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
  •