Hi there. I've written a small script that I use privately on my server as a file upload and temporary storage bin. In other words, I open this page, upload any files I want, and then be able to download them from another location.
It beats E-mail, and I can see the files from any machine.
Anyway, I wanted to add a quick "log in" for it. Just a password, not a username/pass combo. There are no users, just a password.
I've got all that working just fine, but here is the issue. When the page loads, I want everything to display on the page, but have a couple features disabled if they are not logged in. They can see the files and what not, they just can't add more or delete them.
To accomplish this, I created a function which returns plain text that I use later in the page when showing forms and such.
For one example, inside the submit button you can use the keyword "disabled" and your button will not work. So basically all I did was say (in the function) IF logged in, then $status = "disabled". Down in my form I simply type it out with that variable:
echo "<input $status type=........". If they are logged in, $status is just a blank, if not logged in, it equals "disabled". It's pretty simple and dirty.
Here is the main problem, I use a while loop to display the contents of the temp folder. Each time it loops, I show one of the files with a little delete button next to it. Of course, if they aren't logged in, I want that button disabled. Only form elements have the "disabled" keyword, so I can't use the same tactic on an <img> or <a> tag (I don't think).
To accomplish the same basic goal as above, I set my $status variable to the onClick event. onClick for the <img> tag is where I use javascript to eventually delete the file. So I figure, hey, why not just set $status to the whole onClick text? So in other words, my original <img> might be:
echo "<img src=\"bla\" onClick=\"del_file($file);\" />";
Notice there is a variable used in the onClick event, this is part of the while loop, it tells jscript what the file name so it can be deleted. I had the brilliant idea that up in my function I could do the same as before. When logged in I would just set $status to the entire onClick text, and if not logged in, leave it blank. This way the <img> tag would have no click event and thus files couldn't be deleted.
I thought there were two ways to do this, I would make onClick to a string like:
$status = " onClick=\"del_file($file);\" ";
This doesn't work, because it tries to evaluate the $file variable, and it can't because this is in a function and my while loop hasn't even run yet. So maybe I escape the variable:
$status = " onClick=\"del_file(\$file);\" ";
Then it doesn't get evaluated while setting the text.
In any case, the function works, and the loop works, but what doesn't work is trying to get this text to appear in the PHP code and be evaluated properly with that $file variable. I tried just echoing the text:
echo "<img src=\"bla\" " . $status . " />";
This doesn't work because it won't evaluate the variable, it will literally send the text "$file" to the javascript function.
Then I thought eval() would work, this is what it's FOR anyway, right? To evaluate a string as php code?
echo "<img src=\"bla\" " . eval($status) . " />";
And therein lies my question, I can't get eval to work. And again it has to do with using the $file variable. When I create the string up in my function, $file is not even initialized yet, it can only be a string and not evaluated. But later on with eval() checks it, I just get errors. $file not initialized. Or it sends "$file" to javascript, or something else happens. I get parse error in the "eval'd" code, etc...
Here is another problem, I've been reading page after page about how to use eval, and in every example, whenever the strings are created, you know $string = "text to eval later", they always have their variables set first. Like:
$var1 = "joe";
$var2 = "bill";
$eval = "hey \$var1 and \$var2";
echo eval($eval);
Whatever it might be. In my case, I need to create the eval text, using variables, but the variables are not created yet, but they ARE created when I actually use the eval() function on the string. More like this:
$eval = "hey \$var1 and \$var2";
$var1 = "joe";
$var2 = "bill";
echo eval($eval);
If any of this makes sense at all, tell me what I'm missing here. All I want to do is turn the php text " onClick=\"del_file($file);\" " into a string that will work with eval(). Is it really that hard?
I suppose there are other ways to do this, I could just make two statements, and echo one if logged in and one if not, but then every time WHILE loops, I have to check if they are logged in, which wastes time. I can check just once, and set a variable to use in each loop, if possible. Is there some other way?
Thanks