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:


// 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 being Directory. It also needs to be in the next POST variable, &c

Can someone tell me what I’m doing wrong ?

Well first of all you’re sending it as a GET request. So if you check for POST variables, it will never be set.


oRequest.open("GET",sURL,false);

Another issue I see is that you’re not sending any variables over? What are you checking for on the PHP side?

On the PHP side if you’re checking for a name then it would like something like this:


if (isset($_POST['name'])) {
  echo $_POST['name'];
}

And on your javascript side, you will need to send it to the server:


var url = "a_page.php";
var params = "name=JohnDoe";
oRequest.open("POST", url, true);

//Set the proper header 
oRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
oRequest.setRequestHeader("Content-length", params.length);
oRequest.setRequestHeader("Connection", "close");

//Send the params to the server
oRequest.send(params);

So when the server receives the parameters, because you sent a variable called “name” with the value “JohnDoe” then the response will be “JohnDoe” (because we just echoed it out).

Maybe this will make better sense if I post the whole code. The reason it is GET is because I’m getting the contents of a text file, the text file lists the users current directory. It’s easier than using cookies.


// 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 && oRequest.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);
			
		})
	
});	




You see right there at the very bottom ? Thats where the function populateParentWindow actually needs to be called, not up there by the request. But I cannot get the responseText to show up there, it's not showing undefined anymore, but it keeps giving me a blank value, no matter what the text file says. Any ideas ?