trink Posted August 6, 2007 Share Posted August 6, 2007 Basically, I'm very new to php. Like, just started working with it 5 days ago new. I've got a php script that gets the contents of a directory and posts them all as links, and the links look like this: <a href="javascript:document.all.fileclicked.value='$file';dophp()">$file</a> fileclicked is the name of a hidden input on the page. Basically when they click the link it sets the value of the hidden input as $file. Then it calls the dophp() function. The dophp function, in short, runs php code. Within that function I'm trying to get the value of the fileclicked hidden input and set it as a variable. Is it something like $Data = $_Get["fileclicked"]; or something? That's what I'd like to know. (I've tried $_Get and it didnt work, but I'm assuming it could be something close to that) I know that this probably isn't the best way of going about what I'd like to do either, and any tips on cleaning it up would be appreciated. Any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/63495-need-help-getting-a-value-from-a-hidden-input/ Share on other sites More sharing options...
tibberous Posted August 6, 2007 Share Posted August 6, 2007 If I were you, I would make my links like: <a href="pagename.php?file=$file">link</a> Then there is no javascript to deal with, or issues with it not working in certain browsers. Once the link is clicked, the script will have the value of file as $_GET['file'], or just $file if you turn register globals on. Quote Link to comment https://forums.phpfreaks.com/topic/63495-need-help-getting-a-value-from-a-hidden-input/#findComment-316506 Share on other sites More sharing options...
trink Posted August 6, 2007 Author Share Posted August 6, 2007 I tried that... I can see how it would work... it didn't for me. The php looks like this: $File = "filename.txt"; $Handle = fopen($File, 'w'); $Data = $_GET['file']; fwrite($Handle, $Data); fclose($Handle); There are 2 problems. #1: When I clicked a file, it showed the url as index.php?file=filename.here just fine, but it didnt write filename.here to filename.txt. #2: I don't want the file to be written UNTIL the file is clicked. I don't want filename.txt written when the page loads, because I have another application checking for that file, and if it is a file then it reads the contents of that file, does what it needs to do, then deletes filename.txt. So with this, it creates the file when the page is loaded, and has no idea what to do with it because filename.txt has no contents at that point. Quote Link to comment https://forums.phpfreaks.com/topic/63495-need-help-getting-a-value-from-a-hidden-input/#findComment-316516 Share on other sites More sharing options...
tibberous Posted August 6, 2007 Share Posted August 6, 2007 <?php if($_GET['data']){ fwrite($fp = fopen("filename.txt", "w"), $_GET['data']) && fclose($fp); } ?> <a href="<?php echo $_SERVER['php_self']; ?>?data=Link%201">Link 1</a> <a href="<?php echo $_SERVER['php_self']; ?>?data=Link%202">Link 2</a> <a href="<?php echo $_SERVER['php_self']; ?>?data=Link%203">Link 3</a> Quote Link to comment https://forums.phpfreaks.com/topic/63495-need-help-getting-a-value-from-a-hidden-input/#findComment-316529 Share on other sites More sharing options...
trink Posted August 6, 2007 Author Share Posted August 6, 2007 Alright, that all works perfectly, but there's only one problem. Say the file's name is you're.txt, well, it sets the url as index.php?data=you and ends where the ' was. I tried using <a href='index.php?data=str_replace(\"'\", \"%27\", $file)' and I was correct in assuming it wouldn't work. How would I avoid this problem and allow it to set the url as index.php?data=you're.txt? Quote Link to comment https://forums.phpfreaks.com/topic/63495-need-help-getting-a-value-from-a-hidden-input/#findComment-316561 Share on other sites More sharing options...
trink Posted August 6, 2007 Author Share Posted August 6, 2007 And I've also found that if the file has & in it it won't write to filename.txt correctly. Such as 1 & 2.txt will only be written as 1. Quote Link to comment https://forums.phpfreaks.com/topic/63495-need-help-getting-a-value-from-a-hidden-input/#findComment-316647 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.