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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Execute code in textarea.

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello everybody.
I am a beginner in PHP. In a form (textarea) I want to receive text from a txt file. As far as I knew I thought of the read file function but it doesnt work because in the textarea it didn't execute the code but just displayed the code.
I hope you understand what I mean and can tell me how to do it.
Thanks in advance.
Robert Jansen (Mr.Jansuh)
 
Hmm not sure what you mean by this. If you mean you want to include pre-made text files that contain PHP code, then you can simply use an include function.
Say I have a file called includefile.php. To include this, simply place the following line in your PHP code where you want to stick in the file:
[tt]include("includefile.php");[/tt]
If includefile.php is stored in a different folder than the file into which you're including it is stored, you can use that information as well:
[tt]include("mystuff/include/files/includefile.php");[/tt]
Or even:
[tt]include("/home/web/somefolder/includefile.php");[/tt]
This does not require file pointers because I don't think you'll be directly manipulating whatever's inside the text file, and you don't want to display it but want to actually execute it.

Forms are used to get input from a user through the web browser interface. They stick in some information, their information gets stored somewhere, and then can be accessed and viewed. They're not really a server-side mechanism.

And if your code is simply being displayed and not executed, check
A) your server supports PHP, and
B) your code is contained within <?PHP and ?> delimiters.


Hope this helped.
 
Ok. Thanks already for your reaction on this item. What my plan is is that the user enters(submits) a text in the textarea in the form and after a while he can request the same text in it again and modify it. I managed to get the submit and store function but I would really like to be able to display the text later on again.
I hope you know what I mean...
I understand that form is normallly for user to input text but I was hoping this is possible...
Thanks a lot
Robert Jansen (Mr.Jansuh)
 
Couple of things here...

First off, unlike <input> form tags where you specify a value='something' attribute, textarea data resides between the <textarea> and </textarea> delimiters. So, if you want to specify default text, you'd do something like:
<textarea name='blah' cols=50 rows=10>
<?php
/* Your code */
?>
</textarea>

Second. Instead of saving the data to a flat text file as you mentioned above, it may be best to input it into a database. You can then use a session library like phplib ( to keep track of the information ... This is more economic in execution time, disk space and will push you along that learning curve path in the right direction.

Hope this helps.

brendanc@icehouse.net
 
Thanks for the Database tip. As a sasid I am a newbie. Learning php4 from PHP4 in 24 hours. That textarea thing doesnt work unfortunally. It still show the code. I tried it but it doesnt work :-(

I really hope there is a solution for it..
See ya and thanks a lot
Robert
 
Well here's a way with cookies, which might be your best way if you A)don't need a database to actually store on the server what the user inputted, and B)if you don't really want to learn sessions, which isn't that hard but cookies are really rather simple in PHP. Actually, even if you do want to store the input into a database, cookies can handle things locally as well.
First you have the page with input. At its simplest, it can look something like this:

formpage.php
[tt]
<HTML>
<HEAD>
<TITLE>Textarea Page</TITLE>
</HEAD>
<BODY>
<FORM action=&quot;cookiepage.php&quot;>
<TEXTAREA name=formbody cols=45 rows=8>
<?PHP
[/tt]$userbody=$HTTP_COOKIE_VARS[&quot;cookiebody&quot;];[tt]
/*That's your cookie. It checks to see if a cookie storing the user's body exists on the user's hard drive, and if so, it displays it in the textarea*/
echo $userbody;
?>
</TEXTAREA>
</FORM>
</BODY>
</HTML>
[/tt]

And here's the page the form will pass the data to:

cookiepage.php
[tt]
<?PHP
//$formbody=strip_tags($formbody)
/*Above is in case you want to remove HTML from the user-submitted text (requires PHP4 or some regex thing I don't want to get into)*/
if($formbody)
{
[/tt]setcookie(&quot;cookiebody&quot;,$formbody,time()+2592000);[tt]
/*This sets the cookie on the user hard drive. If one is already available, it is overwritten, and if one is not already available, a new one gets written. The value in $formbody is stored, and the cookie is given the name of &quot;cookiebody&quot;, and the cookie is set to expire a month from now.*/
}
<HTML>
<HEAD>
<BODY>
<TITLE>Cookie Page</TITLE>
</HEAD>
<BODY>
Thank you for submitting the following:
<P>
<BLOCKQUOTE>
<?PHP
echo $formbody;
?>
</BLOCKQUOTE>
<P>
CLick <A href=&quot;formpage.php&quot;>here</A> to go back to the form.
</BODY>
</HTML>
[/tt]

There you have it. Feel free to post questions if you have any, and good luck if you don't.
 
Admittedly, I haven't seen the problem you're running into before, Mr. Jansuh. the <?php ?> delimiters should escape from the HTML processing regardless of the HTML around them. However, the first thing I'd do to find a workaround is include the <textarea> </textarea> tags in your PHP echo statements... therefore:

<?php
echo &quot;<textarea>\n&quot;;
/* more code */
echo &quot;</textarea>\n&quot;;
?>

brendanc@icehouse.net
 
Ok guys.. As a beginner and only in lesson 13 of the 24 of sams teach yourself php4 in 24 hours. I don't understand anything anymore :-( To explain the problem better:
I am making a program in PHP which I wnat to use for Costumers inw ebdesign that want to text update their own wbesite without any difficult things to do for them. So now I am making a site for them that they cvan enter the text. The text is being saved to a text file and the text fle is being displayed on the website. I want to be able as soon as they want to change it again that they see th eold text and can modify it.
The trials I've done can be seen at Text files and display pages you can see in the directory

Thanks a lot guys
See ya,
Robert
 
Oh hehe...
That's not your PHP code you're seeing. That's the parser outputting errors to the screen, and its code is being reflected ASCII through the textarea instead of as html. So basically it can't find the file you're trying to open.
 
Either that or, as I just checked since the file does exist, you didn't set the permissions properly.
 
Oh okay, (btw sorry I'm spamming =] ) I just quickly browsed your viewable directories, and I see two problems:
1.) In your demo file ( ), where you're getting the stuff inside the textarea, the location of the page1.txt file has not been set correctly. It should be (for example) ../page1.txt.
1.) In your pageX files ( for example), you're attempting to write to the text files, and that requires you to set the file permissions to writable.
Hope that helps, feel free to ask questions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top