Jump to content

AJAX/PHP problem


S Murder

Recommended Posts

I'm able to get the following code to work:

//This code displays the file lists		function refreshFileList(newListitem)		{			client = GetXmlHttpObject();			listitem = newListitem;			list = this.listitem.getElementsByTagName("ul")[0];			listitem.childNodes[0].innerHTML = "-";			client.open("POST", "listfiles.inc", true);			client.setRequestHeader("If-Modified-Since","Sat, 01 Jan 2000 00:00:00 EST");			client.onreadystatechange = dirListUpdate;			var sendData = "dir=" + listitem.id;			client.send(sendData);		}				function dirListUpdate()		{			//alert(client.readyState);			if(client.readyState == 4 || client.readyState == "complete")			{				//alert(client.responseText);				list.innerHTML = client.responseText;			}		}

But this code doesn't:

//This code displays the selected file		function showFile(newListitem)		{			client = GetXmlHttpObject();			client.open("POST", "showfile.inc", true);			client.setRequestHeader("If-Modified-Since","Sat, 01 Jan 2000 00:00:00 EST");			client.onreadystatechange = fileViewerUpdate;			var sendData = "dir=" + newListitem.id;			//alert(listitem.id);			client.send(sendData);		}				function fileViewerUpdate()		{			//alert(client.readyState);			if(client.readyState == 4 || client.readyState == "complete")			{				//alert(client.responseText);				fileViewer.innerHTML = client.responseText;			}		}

I've tracked the problem, and for some reason it seems to be that in showfile.inc (the .inc files are php) the post value of dir has no value. Anybody have any ideas why? Is it probably a Javascript problem or a php problem?

Link to comment
Share on other sites

In your second code, you had an alert (commented out) that would tell you the id for "listitem" but you don't appear to ever be assigning a value to "listitem". Then your send data is defined as "dir=" + newListitem.id;What happens when you alert "newListitem.id" instead of "listitem.id"? Or, what if you, like the first code block, assigned the "newListitem.id" to "listitem.id" and then used "listitem.id" for the send data?

Link to comment
Share on other sites

In your second code, you had an alert (commented out) that would tell you the id for "listitem" but you don't appear to ever be assigning a value to "listitem". Then your send data is defined as "dir=" + newListitem.id;What happens when you alert "newListitem.id" instead of "listitem.id"? Or, what if you, like the first code block, assigned the "newListitem.id" to "listitem.id" and then used "listitem.id" for the send data?
The listitem and newListitem are both <li> elements being passed in the parameters. I just changed the name to newlistitem because that code block requires that the global listitem variable be updated.When the alert is used it correctly shows the id.I've also tried var sendData = "dir=foo"; in the second code block and foo isn't passed to showfile.inc either.
Link to comment
Share on other sites

I've also tried var sendData = "dir=foo"; in the second code block and foo isn't passed to showfile.inc either.
Can you post the code for showfile.inc?
Link to comment
Share on other sites

Can you post the code for showfile.inc?
listfiles.inc (works)
<?php	function make_file_list($dir="")	{		if($dir == "")		{			$dir = $_SERVER["DOCUMENT_ROOT"];		}				$dirAry = scandir($dir);		foreach($dirAry as $value)		{			$fullpath = $dir . DIRECTORY_SEPARATOR . $value;			if(is_dir($value))			{				//Write list item				echo '<li id="'.$fullpath.'" onclick="showInfo(this)" ondblclick="refreshFileList(this)">';				echo '<span onclick="refreshFileList(parent)" class="expandIcon">+</span>';				echo '<img src="media/dir.ico" width="16px" height="16px" alt="Directory"/> ';				echo $value;				echo '<ul></ul>';				echo "</li>\n";			}			elseif(is_file($value))			{				//Write list item				echo '<li id="'.$fullpath.'" onclick="showInfo(this)" ondblclick="showFile(this)">';				echo '<span class="expandVoid"></span>';				echo '<img src="media/file.ico" width="16px" height="16px" alt="File"/> ';				echo $value;				echo '<ul></ul>';				echo "</li>\n";			}			else			{				//Write list item				echo '<li id="'.$fullpath.'" onclick="showInfo(this)" ondblclick="showFile(this)">';				echo '<span class="expandVoid"></span>';				echo '<img src="media/vlc.ico" width="16px" height="16px" alt="Other"/> ';				echo $value;				echo '<ul></ul>';				echo "</li>\n";			}		}	}		make_file_list($_POST["dir"]);?>

showfile.inc (doesn't work) (I changed the post value from "dir" to "file_name").

<?php		//Extension lists	$txtExts = '.css,.cgi,.htm,.html,.js,.log,.php,.php3,.pl,.shtml,.txt';	$imgExts = '.bmp,.gif,.jpg,.jpeg,.ico,.png,.tiff';	$flashExts = '.swf';	$mediaExts = '.acc,.asf,.avi,.mov,.mp3,.mp4,.qt,.wma,.wmv';		function make_txt_editor()	{		echo '<form name="txtEditor" action="java script:saveFile();" method="GET">'."\n";		echo '<textarea></textarea>'."\n";		echo '<input type="submit" value="Save Changes"/>'."\n";		echo '</form>'."\n";	}		function examine_ext($file_name)	{		echo "File name: ".$file_name;		echo "test";		$extIndex = strpos($file_name, '.');		if($extIndex < 0)		{			$ext = '';			echo "no extension";		}		else		{			$ext = substring($file_name, $extIndex);			echo $ext;		}				//If there's no extension		if($ext == '')		{			make_txt_editor();		}		//If the extension is for a txtx file		if(strpos($txtExts, $ext) != -1)		{			make_txt_editor();		}		//If the extension is for an image file		if(strpos($imgExts, $ext) != -1)		{			make_img();		}		//If the extension is for a flash file		elseif(strpos($flashExts, $ext) != -1)		{			make_flash_obj();		}		//If the extension is for another media file		elseif(strpos($mediaExts, $ext) != -1)		{			make_media_obj();		}		//Open in text editor as last ditch effort		else		{			make_txt_editor();		}	}		examine_ext($_POST["file_name"]);?>

In showfile.inc, the 2 echo statements in examine_ext($file_name) work so I know the function is successfully being called, but no value is displayed for file_name.When the echo statement is outside the function, no value is displayed there either.

Link to comment
Share on other sites

You said that you changed the post value from "dir" to "file_name". Did you also change that in the javascript?
Yes. Here's the updated JS:
//This code displays the selected file		function showFile(newListitem)		{			client = GetXmlHttpObject();			client.open("POST", "showfile.inc", true);			client.setRequestHeader("If-Modified-Since","Sat, 01 Jan 2000 00:00:00 EST");			client.onreadystatechange = fileViewerUpdate;			var sendData = "file_name=foo";			//alert(listitem.id);			client.send(sendData);		}				function fileViewerUpdate()		{			//alert(client.readyState);			if(client.readyState == 4 || client.readyState == "complete")			{				//alert(client.responseText);				fileViewer.innerHTML = client.responseText;			}		}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...