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!

print output should go into HTTP/textarea

Status
Not open for further replies.

Roeckli

Technical User
Aug 3, 2002
14
NL
Hi@all,

I would like to read a file into a TEXTAREA/HTTP when the page gets opened. The file will be modified and the changes should be saved in the same file again.

Does anyone know how?

I cant get it 2 work :( me--> newbie :)


Many Thanks

Daniel
 
An example.....

[/code]
#!/usr/local/bin/perl
use strict;
use CGI;
use CGI::Carp 'fatalsToBrowser';
my $text;

my $cgi = new CGI;
print $cgi->header, $cgi->start_html, $cgi->start_form;

# if new content has been submitted, write to disk
if ($text = $cgi->param('input1'))
{
# write new content to OutPut File
open(OPF,">file.txt") or
die "Failed to open file.txt for write, $!";
print OPF "$text";
close OPF;
}
elsif (-e "./file.txt")
{
# open/read InPut File
open(IPF,&quot;<file.txt&quot;) or
die &quot;Failed to open file.txt, $!&quot;;
while (<IPF>) { $text .= $_; }
close IPF;
}

print qq(<p>Show me the text</p>
<textarea name='input1' cols='45' rows='5'>$text
</textarea>
<input type='submit' value='Submit'>);

print $cgi->end_form, $cgi->end_html;
[/code] 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Many Thanks for the reply :)

I've tried it... but i get an (or some more) Errors out.

Do i need to put the Full path here?
elsif (-e &quot;./file.txt&quot;) /var/ ?

Where do i put this Code? And how do i call it?

I'm still reading all the books ;)

Cheers

Daniel
 

Do i need to put the Full path here?
elsif (-e &quot;./file.txt&quot;) /var/ ?


No, './file.txt' means look in the current directory.
It should work fine, just like it is.


Where do i put this Code?

Put the code in a valid cgi-bin directory on your web server.
Guessing from the path in your previous post, it looks like you are running apache on a linux box, maybe. If so, you probably have a directory like /var/ That is where you want to put all of your CGI code.

Also, if I'm guessing correctly about your OS, you'll need to set the execute bits on the code.

So,
1 - copy/paste the text of the code into a file.
2 - Save that file in /var/ as 'simple.cgi'.
3 - While in that dir, do - &quot;chmod +x simple.cg&quot;


And how do i call it?

To run the code, in a browser,
go to &quot;where that_machine is that hostname for your server.


PS - there are a couple of faqs in the CGI forum that might be helpful. 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
THX :))

Thats what i did... Error:premature end of script headers: /var/
Perl simple.cgi Output:

Content-Type: text/html; charset=ISO-8859-1

<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
<!DOCTYPE html
PUBLIC &quot;-//W3C//DTD XHTML Basic 1.0//EN&quot;
&quot;<html xmlns=&quot; lang=&quot;en-US&quot;><head><title>Untitled Document</title>
</head><body><form method=&quot;post&quot; action=&quot;/simple.cgi&quot; enctype=&quot;application/x-<p>Show me the text</p>
<textarea name='input1' cols='45' rows='5'>hiho

Which **hiho** is in the text file :)

does it have to be there?

again THX A LOT

Daniel
 
The 'premature end of script headers' error indicates some basic failure with the code. I just copied/pasted the above code into a text file (simple.cgi) and saved it into my cgi-bin. I then made sure it was not a DOS flavored file with 'dos2unix' and then set the execute bit with 'chmod +x simple.cgi'...... hit it with a browser and it worked.

So, you must have a typo or it has DOS line endings or.... something similar to that type of problem.

You can run the code from the command line,
prompt>perl -e simple.cgi -enter-

and see what kind of output you are getting.... that might supply some clues. 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
I wrote it again and it works :)
THANKS but...
If I put some text in the TextArea those text will be saved in the test.txt(Which is nice) But if I open an other Browser the TEXTAREA is empty :(. My Idea was, to print a config file, which I can modify. to be able to Add or Modify some text. (in the same line or create new line)

any idea?

Daniel
 
When I run that on my machine, if I hit that URL it runs through this 'elsif' which reads the text file and sets $text.
Code:
elsif (-e &quot;./file.txt&quot;)
    {
    # open/read InPut File
    open(IPF,&quot;<file.txt&quot;) or
        die &quot;Failed to open file.txt, $!&quot;;
    while (<IPF>) { $text .= $_; }
    close IPF;
    }
The subsequent printing of the textarea (with it's content in $text) shows stuff that was read from the text file and stored in $text.
Code:
print qq(<p>Show me the text</p>
   <textarea name='input1' cols='45' rows='5'>$text
   </textarea>
   <input type='submit' value='Submit'>);


Is that not doing what you are asking for??? 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
IT DOES 100% :))) Many thanks goBoating.

I should learn how 2 RTFP ;) May ask you one more question?

The CGI script is now in my HTML Page. If I submit the changes it points me to the /cgi-bin/simple.cgi....

Is there a way to stay in the HTML page?

again THX!

Daniel
(yeahhhhh)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top