www.webdeveloper.com
Results 1 to 4 of 4

Thread: XMLHttpRequest help !

  1. #1
    Join Date
    Dec 2010
    Posts
    33

    XMLHttpRequest help !

    I'm having a little trouble arranging this code so my variable is defined, according to firebug, it is showing up undefined in the POST variables. Heres my code:

    Code:
    // Setup our object
    var oRequest=new XMLHttpRequest();
    var sURL  = "../../currentdirectory-<?php echo $_COOKIE['User']; ?>.txt";
    
    // When a request comes in, the state is going to change. So when that happens
    // kick off the following function
    oRequest.onreadystatechange=function()
    {
      if (oRequest.readyState==4 && xmlhttp.status==200)
      {
           // The request came in and is ok, so pass the response text to our function
           populateParentWindow(oRequest.responseText);
      }
    } 
    
    // Ok, we are set with handling the response, now go ahead and send the request
    oRequest.open("GET",sURL,false);
    oRequest.send(null);
    
    
    $(function(){	
    	 function populateParentWindow(Directory) {
         $("#FTPContents").load('functions/testftp.php?HASH=' + Get_Cookie("ftphash") + "&reload=" + Directory) ;
    	 }
    	$('#swfupload-control').swfupload({
    		upload_url: 'functions/upload-file.php?UID=' + Get_Cookie("User") + "&c=",
    		file_post_name: 'uploadfile',
    		file_size_limit : "10000",
    		file_types : "*.*",
    		file_types_description : "All Files",
    		file_upload_limit : 5,
    		flash_url : "functions/js/swfupload/swfupload.swf",
    		button_image_url : 'functions/js/swfupload/wdp_buttons_upload_114x29`.png',
    		button_width : 114,
    		button_height : 29,
    		button_placeholder : $('#button')[0],
    		debug: false
    	})
    The variable that needs to be passed is of course 'Directory'. This variable also needs to be passed to the upload-url: variable, but I knew there was no sense in trying because it's not working a few lines above that. Can someone tell me what I'm doing wrong ?

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by SyncUpSolutions View Post
    I'm having a little trouble arranging this code so my variable is defined, according to firebug, it is showing up undefined in the POST variables. Heres my code:
    You're using GET not POST.

    if (oRequest.readyState==4 && xmlhttp.status==200)
    What is xmlhttp?

    Code:
    if ( this.readyState==4 && this.status==200 )
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  3. #3
    Join Date
    Dec 2010
    Posts
    33
    Thats because I'm getting the contents of a text file. The obRequest.responseText is a name of a directory, I need that to be passed into
    populateParentWindow(Directory), this is the problem.

    Let me post all of the code:

    Code:
    // Setup our object
    var oRequest=new XMLHttpRequest();
    var sURL  = "../../currentdirectory-<?php echo $_COOKIE['User']; ?>.txt";
    
    // When a request comes in, the state is going to change. So when that happens
    // kick off the following function
    oRequest.onreadystatechange=function()
    {
      if (oRequest.readyState==4 && xmlhttp.status==200)
      {
           // The request came in and is ok, so pass the response text to our function
           populateParentWindow(oRequest.responseText);
      }
    } 
    
    // Ok, we are set with handling the response, now go ahead and send the request
    oRequest.open("GET",sURL,false);
    oRequest.send(null);
    
    
    $(function(){	
    	 function populateParentWindow(Directory) {
         $("#FTPContents").load('functions/testftp.php?HASH=' + Get_Cookie("ftphash") + "&reload=" + Directory) ;
    	 }
    	$('#swfupload-control').swfupload({
    		upload_url: 'functions/upload-file.php?UID=' + Get_Cookie("User") + "&c=",
    		file_post_name: 'uploadfile',
    		file_size_limit : "10000",
    		file_types : "*.*",
    		file_types_description : "All Files",
    		file_upload_limit : 5,
    		flash_url : "functions/js/swfupload/swfupload.swf",
    		button_image_url : 'functions/js/swfupload/wdp_buttons_upload_114x29`.png',
    		button_width : 114,
    		button_height : 29,
    		button_placeholder : $('#button')[0],
    		debug: false
    	})
    		.bind('fileQueued', function(event, file){
    			var listitem='<li id="'+file.id+'" >'+
    				'File: <em>'+file.name+'</em> ('+Math.round(file.size/1024)+' KB) <span class="progressvalue" ></span>'+
    				'<div class="progressbar" ><div class="progress" ></div></div>'+
    				'<p class="status" >Pending</p>'+
    				'<span class="cancel" >&nbsp;</span>'+
    				'</li>';
    			$('#log').append(listitem);
    			$('li#'+file.id+' .cancel').bind('click', function(){
    				var swfu = $.swfupload.getInstance('#swfupload-control');
    				swfu.cancelUpload(file.id);
    				$('li#'+file.id).slideUp('fast');
    			});
    			// start the upload since it's queued
    			$(this).swfupload('startUpload');
    		})
    		.bind('fileQueueError', function(event, file, errorCode, message){
    			alert('Size of the file '+file.name+' is greater than limit');
    		})
    		.bind('fileDialogComplete', function(event, numFilesSelected, numFilesQueued){
    			$('#queuestatus').text('Files Selected: '+numFilesSelected+' / Queued Files: '+numFilesQueued);
    		})
    		.bind('uploadStart', function(event, file){
    			$('#log li#'+file.id).find('p.status').text('Uploading...');
    			$('#log li#'+file.id).find('span.progressvalue').text('0%');
    			$('#log li#'+file.id).find('span.cancel').show();
    		})
    		.bind('uploadProgress', function(event, file, bytesLoaded){
    			//Show Progress
    			var percentage=Math.round((bytesLoaded/file.size)*100);
    			$('#log li#'+file.id).find('div.progress').css('width', percentage+'%');
    			$('#log li#'+file.id).find('span.progressvalue').text(percentage+'%');
    		})
    		.bind('uploadSuccess', function(event, file, serverData){
    			var item=$('#log li#'+file.id);
    			item.find('div.progress').css('width', '100%');
    			item.find('span.progressvalue').text('100%');
    			var pathtofile='<a href="uploads/'+file.name+'" target="_blank" >view &raquo;</a>';
    			item.addClass('success').find('p.status').html('Done!!! | '+pathtofile);
    		})
    		.bind('uploadComplete', function(event, file){
    			// upload has completed, try the next one in the queue
    			$(this).swfupload('startUpload');
    			populateParentWindow(oRequest.responseText);
    			
    		})
    	
    });
    See there at the bottom ? populateParentWindow(oRequest.responseText) ? Thats where I actually need the function to be called, not up at the top of the code near all the requests. Because that is when the upload is done, and it refreshes the FTP Contents window without refreshing the whole page. But the oRequest.responseText shows up empty down there.

  4. #4
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    The server request you're trying to make is synchronous so it should be integrated into the populateParentWindow function, after having made the correction that I gave, as currently it must be generating a console error.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles