Jump to content

need help getting a value from a hidden input


trink

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.