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

Creating Default field values 1

Status
Not open for further replies.

JaybOt

Programmer
Apr 18, 2001
101
0
0
GB
Hi all,

I was just wondering, how can i open a file in a browser, and read the file contents into emty fields. e.g.

if i have an file like inetd.conf in the usual format, how do i read each line and put it into a field as the default, selecting submit on this form will then save a new file (backing up the old) writing each field on a new line, essentially re-writing my inetd.conf with any new values.

Thanks in advance.

JayBot@jamsoft.uk.net
work, FRAG, eat, FRAG, sleep, FRAG ....

 
in the perl script, open the file, then either read the data into an array to be used later or have a 'while' loop on the filehandle, doing the work needed right there:[tt]
open(FILE, "file.txt") or dienice("Failed to open: $!");
while (<FILE>)
{
print &quot;<tr><td> $_ </td></tr>\n&quot;;
}
close FILE;[/tt]

the array version of this would look almost the same, except it would be a 'foreach' loop done over the array. &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Hey thanks again for you responce, just another quick one ... can you just explain the line ...

print &quot;<tr><td> $_ </td></tr>\n&quot;;

as i am new to perl. Doses this also setup the field? and how can i save this file?

cheers!



JayBot@jamsoft.uk.net
work, FRAG, eat, FRAG, sleep, FRAG ....

 
well, that's mostly not perl, it's html. 'print' prints the stuff to the html page(if called as a cgi script). '\n' is a newline character. '$_' is a special variable, which, in the while loop, is set to the value of whatever is in the conditional statement that controls the while loop(in this case, each line from the file). the rest is just the printing of a row for each element. you'll of course have to start the '<table>' before this loop, and end it afterwards.

as for 'how can i save this file', um, er... well, it's not making a file, it's just printing to standard output (the default for 'print'). in the case of a cgi script, standard output is sent to the viewer's browser(simplification). you could save this to a file if you wanted, but since you already have the file you're reading from, and the processes you're performing on it being so minimal, i wouldn't see much need. well, regardless, to do it open up the file you want to save it as and call print like this:[tt]
print OUTFILE &quot;whatever\n&quot;;[/tt]
&quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Thanks stillflame for this help, only i should have explained a little better what i wanted to do.

What i want to do is read each LINE of text from a file into a long text field, 30 chars or so, so i have 1 line of text per field. each field on a new line.

maybe i can use the code you posted, but use @line to read each line of data.

Thanks!
 
oh, i see, you want to be able to edit the file through the webpage...

well, you'll need to print out something like this:[tt]
<form blah blah blah>
<input type='text' length=30 name='1' value='put string of text that is the file here'>
<input type='text' length=30 name='2' value='put string of text that is the file here'>
etc.
</form>
[/tt]
you can use the same while loop for this, but add this line to the top of it:[tt]
chomp;
[/tt]
&quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top