Gossamer Forum
Home : General : Perl Programming :

Re-displaying data back into a form

Quote Reply
Re-displaying data back into a form
Ok, I have a form consisting of 4 text boxes. When the form is submitted, the data is wrote to a temp dat file separated by a "|". My main problem is the last text box. It's actually a multi-line textarea box, but it only shows the first "line" (up until you hit the enter button) not the whole data as entered the first time. Here's the file format:

Code:
Test Test Test Test Test Test Test Test Test Test<p>
<p>
Test Test Test Test Test<p>
<p>


The above doesn't show right on here, but each line is separated by "<p>/n<p>/n" (paragraph, new line, paragraph, new line)

Now, I have the following to put this data back into the text box:

Code:


$eurom =~ s/<p>/\r/g;


<TEXTAREA name="eurom" rows=6 wrap=virtual cols=60>$eurom</TEXTAREA>




I've tried alot of variations of this but nothing has worked. (regex I mean)

No matter what I try, it will not work. It only shows the first "line" of the data.

Any more ideas?

Last edited by:

manunkind: Mar 16, 2002, 10:33 AM
Quote Reply
Re: [manunkind] Re-displaying data back into a form In reply to
Nevermind, the CODE button doesn't even work on this forum. Unsure
Quote Reply
Re: [manunkind] Re-displaying data back into a form In reply to
It does if you use it properly :)

Code:
hello

Last edited by:

Paul: Mar 16, 2002, 10:18 AM
Quote Reply
Re: [Paul] Re-displaying data back into a form In reply to
I see that. I'm not sure what I did but I got the first post corrected.

Anyway, my form would work like a web forum when you click to EDIT your post. But my problem is that it only shows the first line in the textarea box. It will not display it as it was entered.
Quote Reply
Re: [manunkind] Re-displaying data back into a form In reply to
Trying changing the \r here to \n:

Code:


$eurom =~ s/<p>/\r/g;

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: [jagerman] Re-displaying data back into a form In reply to
Thanks Jason, but that's the first thing I tried. It has no change at all. I've tried ALOT of variations to this regex, but nothing has worked.

Code:


$eurom =~ s/<p>/\n/g;


$eurom =~ s/<p>\n/\n/g;


$eurom =~ s/\n/\r/g;
Quote Reply
Re: [manunkind] Re-displaying data back into a form In reply to
Are you storing actual \n's in the file? This could definately throw things off.

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: [manunkind] Re-displaying data back into a form In reply to
Here's some more history behind the "<p>" tag being in there. This gets displayed on an .shtml page, so I needed to change all "\r" to "<p>" tags to display properly. Here's the regex I used:

Code:


$eurom =~ s/\r/<p>/g;


So, I figured I could just reverse the regex, but nothing works.

I'm totally out of ideas.

Last edited by:

manunkind: Mar 16, 2002, 1:14 PM
Quote Reply
Re: [jagerman] Re-displaying data back into a form In reply to
Yes, the file format is in my first post. Each line is separated by "<p>/n"

Last edited by:

manunkind: Mar 16, 2002, 1:14 PM
Quote Reply
Re: [manunkind] Re-displaying data back into a form In reply to
How do you read the file? If you read line by line, you will obviously have a problem since the record is not all on one line. For example, let's say the file is like this:

Username|Subject|Body

Code:


jagerman|Test Post - only one line|asdfasdf
jagerman|Test Post 2 - multiple line body|asdfasdf<p>
asdfasdf<p>
asdfasdf


You see the problem? If I read line by line, I'll get the first post correctly, but when I read the second line, I'm only going to get:

jagerman|Test Post 2 - multiple line body|asdfasdf<p>

What you need to do is do something about the \n's before you write the post to the file. If the <p>'s are there, as it seems they are, you can simply remove them. Then, the above example will look like this in the file:



Code:


jagerman|Test Post - only one line|asdfasdf
jagerman|Test Post 2 - multiple line body|asdfasdf<p>asdfasdf<p>asdfasdf


Now, when you read the second line, you'll get the complete record:

jagerman|Test Post 2 - multiple line body|asdfasdf<p>asdfasdf<p>asdfasdf

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: [jagerman] Re-displaying data back into a form In reply to
I'm also assuming a small square in a text file is a newline (\n), right?

Anyway, yes they are in there.
Quote Reply
Re: [manunkind] Re-displaying data back into a form In reply to
Notepad is picky - it will not show \n as a new line but requires \r\n. Most editors, and more importantly, Perl, will see both \r\n and \n as new lines. (NB - this applies only on Windows systems).

While you may see it all on one line in Notepad, Perl will see it as several lines. You should probably consider switching to an editor such as EditPlus.

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com

Last edited by:

jagerman: Mar 16, 2002, 1:23 PM
Quote Reply
Re: [jagerman] Re-displaying data back into a form In reply to
Jason, you are 100% on to the problem, but I might not know enough about Perl to fix it. Here's the code I use to read it and assign to the variables:

Code:


unless
($action) {
open (TEMP,
"$filename2") or die "Can't Open $filename2: $!\n";
$line = <TEMP>;
close(TEMP);
($dt, $time, $topic, $eurom) = split(/\|/, $line);
$eurom =~ s/\n/\r/g;
&print_form;

}
Am I way off?? I tried using arrays (@line) but I got a 500 error.

Last edited by:

manunkind: Mar 16, 2002, 1:23 PM
Quote Reply
Re: [manunkind] Re-displaying data back into a form In reply to
When you write the post to the file, do this:

$post =~ s/\r?\n/<br>/g;

That will convert all of the newlines to <br>'s. You can adjust as needed - you might be able to leave out the <br>.

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: [jagerman] Re-displaying data back into a form In reply to
Got it! You are awesome, thank you very much Jason!! Smile