Page 1 of 1

help with php and text area?

Posted: Sat Feb 27, 2010 10:56 am
by jameslat
here is my code...

Code: Select all

 
<html>
<head></head>
<body>
<h2>Get er done</h2>
<?php
///by James Lat
if (!isset($_POST['submit'])){
?>
<form action = "<?php echo $SERVER['PHP_SELF']; ?>" method="post">
Upload your .txt file: <input name="myfile" size="10">
<input type="submit" name="submit" value="Upload">
</form>
<--help here-->
<form>
<form method="post"> 
<textarea cols="50" rows="4" name="edit"></textarea>  
</form>
<?php
}
else{
$myfile=$_POST['myfile'];
include("$myfile"); 
}
?>
</body>
</html>
 
I need some help. this program will ulpoad a .txt file, however i'm trying to make it uplaod the file to a text area.
how can i do this?
thanks to all! :D

Re: help with php and text area?

Posted: Sat Feb 27, 2010 11:41 am
by califdon
jameslat wrote: i'm trying to make it uplaod the file to a text area.
how can i do this?
thanks to all! :D
I don't know what that means. A text area is an HTML element. "Uploading" a file means making a copy of the file on the server. There is no connection between the two. What is it you are trying to do?

Re: help with php and text area?

Posted: Sat Feb 27, 2010 12:02 pm
by jameslat
ok i shouldn't say upload bc that's wrong terminology.
basicaly it's just grabing a file form on the server.

Code: Select all

 
<html>
<head></head>
<body>
<h2>Get er done</h2>
<?php
///by James Lat
if (!isset($_POST['submit'])){
?>
<form action = "<?php echo $SERVER['PHP_SELF']; ?>" method="post">
Upload your .txt file: <input name="myfile" size="10">
<input type="submit" name="submit" value="Upload">
</form>
<form>
<form method="post"> 
<textarea cols="50" rows="4" name="edit">
<?php
}
else{
$myfile=$_POST['myfile'];
include("$myfile"); 
}
?>
</textarea>  
</form>
</body>
</html>
 
i revised the code a little bit. basicaly i'm trying to get the file to be laoded into the text feild.
if u run this code as is it will load the text file, but not in the textarea box.
how can i make the file laod in the text area box?
thanks again.

Re: help with php and text area?

Posted: Sat Feb 27, 2010 12:17 pm
by califdon
You'll have to write the code that determines where the uploaded file is to be stored on the computer, including what filename it will have, then you can include that file in the html textarea. There's a fair amount of php code required to do that. Read a tutorial about uploading files with php. There are a lot of decisions you'll have to make, like whether you're going to keep these files on the server forever, how you're going to name them, etc.

Re: help with php and text area?

Posted: Sat Feb 27, 2010 12:22 pm
by jameslat
ok, but i'm not tying to uplaod a file.
i'm trying to take an existing file (that's already on the server) and load the code to a textarea feild.
how can i do this?
thanks.

Re: help with php and text area?

Posted: Sat Feb 27, 2010 1:30 pm
by califdon
Your code says "Upload your .txt file" and your submit button says "Upload", but now you're saying you're not trying to upload a file? Could have fooled me.

So I have to guess that you're doing this on your own, local server. Otherwise, you couldn't expect a web visitor to know what filename and path to specify, right? In that case, all you need to do is use PHP to include the text file in the HTML textarea element (between the opening and closing tags):

Code: Select all

<?php
$filename = isset($_POST['myfile'] ? $_POST['myfile'] : "";
...
?>
...
<textarea rows=12 cols=40><?php include($filename);?></textarea>
...

Re: help with php and text area?

Posted: Sat Feb 27, 2010 1:57 pm
by jameslat
it keeps telling me that i have a parse error?

Re: help with php and text area?

Posted: Sat Feb 27, 2010 1:59 pm
by jameslat
it keeps telling me that i have a parse error?
how could i get the file to read it into the text area?

Re: help with php and text area?

Posted: Sat Feb 27, 2010 6:08 pm
by mikosiko
in this line:

Code: Select all

$filename = isset($_POST['myfile'] ? $_POST['myfile'] : "";
add a ) before the ; like this

Code: Select all

$filename = isset($_POST['myfile'] ? $_POST['myfile'] : ""[color=#FF0000])[/color];
and test
Miko

Re: help with php and text area?

Posted: Sat Feb 27, 2010 6:12 pm
by califdon
Yup, even us old-timers slip up now and then. But mikosiko got the wrong place to add the parenthesis. It needs to go before the "?" like this:

Code: Select all

$filename = isset($_POST['myfile']) ? $_POST['myfile']) : "";
because it's the closing parenthesis for the isset() function.

To help you with coding PHP, think of error messages as your friends! When it says "parse error" it's telling you that it can't understand what you want because you have not followed the syntax rules. So look for things like missing semicolons (;) at the end of lines, or unmatched brackets or parentheses (in this case) or only part of the statement, etc. Then you will find your errors fast. Since I was doing that one in my head, my head didn't give me any parse error.

Re: help with php and text area?

Posted: Sat Feb 27, 2010 6:43 pm
by mikosiko
califdon wrote:Yup, even us old-timers slip up now and then. But mikosiko got the wrong place to add the parenthesis. It needs to go before the "?" like this:

Code: Select all

$filename = isset($_POST['myfile']) ? $_POST['myfile']) : "";
because it's the closing parenthesis for the isset() function.

To help you with coding PHP, think of error messages as your friends! When it says "parse error" it's telling you that it can't understand what you want because you have not followed the syntax rules. So look for things like missing semicolons (;) at the end of lines, or unmatched brackets or parentheses (in this case) or only part of the statement, etc. Then you will find your errors fast. Since I was doing that one in my head, my head didn't give me any parse error.
nah... just testing if you were awake ... (no really.. I had a brain f a r t )

I lost one point

Re: help with php and text area?

Posted: Sat Feb 27, 2010 9:55 pm
by califdon
Don't feel bad, mikosiko, it happens to everyone. :-) Thanks for contributing!