Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing display file on link click. 1

Status
Not open for further replies.

ErrorLINE1CHAR1

Programmer
Dec 3, 2010
24
US
Hello again people of TT...

I'm having an issue, I have been losing sleep for the past couple of days between reading hundreds of forums,
and trying what seems like thousands of different attempts to change the displayed file when you click on a link.
It may sound simple but its actually complex as I am not changing the page I'am simply trying to $_POST the value
of an array to a variable. let me demonstrate..(I hope i don't confuse people :/ )

Code:
<?php
$ptf = "path/to/file/";
$lines = file($ptf);
$files = read_folder_directory ("path");

function chngFile() {
    $ptf .= $file;
}

//The function to create the array from the directory.
function read_folder_directory($dir = "./path/path") { 

$listDir = array(); 

	if($handler = opendir($dir)) { 
		while (($sub = readdir($handler)) !== FALSE) { 
			if ($sub != "." && $sub != ".." && $sub != "Thumb.db" && $sub != "Thumbs.db") { 
				if(is_file($dir."/".$sub)) { 
					$listDir[] = $sub; 
				} elseif(is_dir($dir."/".$sub)){ 
					$listDir[$sub] = ReadFolderDirectory($dir."/".$sub); 
				} 
			} 
		} 
	closedir($handler); 
	} 
	return $listDir; 
}

//List each file as a link.
foreach ($files as $file) {
    echo "<a href='' name='link' onclick='document.forms('postfrm').submit(); return false;' onsubmit='chngFile();' >" . $file . "</a>";
		
}

//Read each line of the chosen $file, and output it.
foreach($lines as $line_num => $line) {
    
    echo "htmlspecialchars($line)";
}

?>

So there is the basic of it, (I assure you any variables ^up^ there that are not there are in the actual source.)
But my problem is that i cant figure out how to change the point in the array based on the name $file in the link..
the only thing it does right now is load the page and display the contents of "test.txt".
(test.txt is in the directory also the last point of the array.)

I have tried to $_POST['link']; but I don't think that that is possible to get that value from an echo..
Im not sure how i could provide a none echo'd link with the value of the foreach though..?

The onclick event works and self submits but only reloads test.txt;
The onsubmit I have tried a few different ways including removing it, putting it in
a function that does the same thing then putting it in the onclick event.. still nothing..

I'm not even sure if what i am doing is possible it could be with JavaScript but i know just about 0 of that..
Maybe I'm going about this the wrong way but I have zero help from any of my coding buddies because they are all
busy on other parts of our App..(the easy stuff)

Oh and before I forget there is ZERO errors.
And please if you have a question DO ASK! I'm well aware that this is probably the most confusing post you have ever seen..
(Its hard to be a genius that thinks a million miles a second, and try to put all though process into a single query... :( )

I will link to a live demo but only via PM as it is NOT open for the public..

Thanks for all your help!!

Devon
Intel Corp. (HF)
 
Php does not interact with the client directly. To change things client side you must use JavaScript.

It looks like your js is wrong too. Both structurally but more importantly from an approach perspective. Please ask in the JavaScript forum for more info.

I suspect you need to include the full path of each file in the returned listDir array. Personally I would store the array in a session variable and then plot the key in an unordered list. Then apply an onclick listener to the list elements.
 
thanks Padie!! I'll ask around there too if I cant figure out how to do your suggestion, care to post a quick example of storing an array in $_SESSION? (you dont have to include anything from my source just a general example.)

As far as the JavaScript in there it was something I found on google to submit a form via the <a> tag, because like i said I dont know anything about JS.. :/

Devon
Intel Corp. (HF)
 
wow i figured it out.. the href='dir.php?file=$file'

then the load is $ptf.= $_GET['file'];

thanks!!!

Devon
Intel Corp. (HF)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top