Forum Moderators: mack

Message Too Old, No Replies

Paragraphs in a Textarea Form

Automatic paragraphs without <p> tag

         

galileo5

3:21 am on Jul 30, 2006 (gmt 0)

10+ Year Member



How can a paragraph tag be created automatically within a textarea of a form?

When I enter content with paragraphs into a form, I want the page to display the paragraphs as originally entered without my having to enter a <p> tag where the paragraphs should be.

coopster

9:04 pm on Jul 30, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You mean you are allowing a user to enter content into a <textarea> and you want to turn around and show that information in a web page, but show any newlines as paragraph elements?

galileo5

9:31 pm on Jul 30, 2006 (gmt 0)

10+ Year Member



Yes, that is correct.

Any advice?

celgins

10:01 pm on Jul 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The only way I know to do this is through scripting, where you must replace paragraph breaks with the <p> tag.

For example, if you know ASP, you could do this:


<%
content=Request.Form("content")
Response.Write (Replace(content, Chr(13), "<p />"))
%>

<html>
<head>
</head>

<body>

<form method=post action=test.asp>
<textarea rows=4 cols=50 name=content></textarea>
<input type=submit value=submit>
</form>

</body>
</html>

Of course the same thing can be done in PHP, or Perl. The "test.asp" feeds itself the content in the form and writes it out with the Chr(13) (a carriage return) replaced by a <p />.

If you do not know how to write in either of these languages, I'm not sure if there is another way to accomplish it.

Anyone else know of a simpler way?

galileo5

10:40 pm on Jul 30, 2006 (gmt 0)

10+ Year Member



I'm actually using PHP. I used the code above, but it isn't working. Here is the code I have on my forms page (with your suggestion):

<%
article=Request.Form("article")
Response.Write (Replace(article, Chr(13), "<p />"))
%>

<html>
<body>

<form action="insert_db.php" method="post">

Title <br />
<input size="50" type="text" name="title" /> <br /><br />

Article <br />
<textarea name="article" cols="50" rows="10"></textarea><br /><br />

<p>
<input type="submit" name="submit" value="Broadcast it!" />
<input type="reset" name="reset" value="Reset" />

</form>

</body>
</html>

Any other advice?

celgins

11:41 pm on Jul 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I've tried the code a number of times and it works fine. In fact, I rewrote your code using PHP. Try the example below:


<?php

$article=$_POST["article"];
$newarticle=str_replace(Chr(13),'<p>', $article);
print $newarticle;
?>

<html>
<body>

<form action="insert_db.php" method="post">

Title <br />
<input size="50" type="text" name="title" /> <br /><br />

Article <br />
<textarea name="article" cols="50" rows="10"></textarea><br /><br />

<p>
<input type="submit" name="submit" value="Broadcast it!" />
<input type="reset" name="reset" value="Reset" />

</form>

</body>
</html>

The string $newarticle is the one with the newly formatted text. Of course, you can rename this to whatever you like.

galileo5

12:26 am on Jul 31, 2006 (gmt 0)

10+ Year Member



I would presume I would use this code on the page that's calling on the data that was entered into the form. I wouldn't use this code on the page that creates the form.

Would that be correct?

galileo5

1:15 am on Jul 31, 2006 (gmt 0)

10+ Year Member



I played around with the code, and it now works.

Thank you, celgins!

celgins

1:15 am on Jul 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Well, I'm not really sure how you have your code setup, but the code you see is actually your entire page: "insert_db.php"

In other words, you could copy/paste that code into a file called "insert_db.php". This page will load in your browser window with the Title and Article fields.

When you type in content and click "Broadcast It", your content will be written to the screen, with the paragraph distinctions.

Of course, the PHP portion contained at the tope (between <?php and?> can be placed in another PHP file if you want.

You would then change the <form method=post action=someotherfile.php> to point to someotherfile.php.

celgins

1:16 am on Jul 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Cool deal. Hope it's beneficial for you!

galileo5

1:33 am on Jul 31, 2006 (gmt 0)

10+ Year Member



celgins,

Our posts must have crossed at the same time, but I just wanted to make sure you saw my thanks.

I got it to work now!

Baruch Menachem

2:06 pm on Mar 26, 2008 (gmt 0)

10+ Year Member



I found this to be great! Only one problem, is 13 really the right number? I am now getting line feeds from the form, rather than line feeds from the user. I am using textarea and every line in texarea is creating a chr(13). I notice that user generated linefeeds are different from textarea line feeds.

rocknbil

5:54 pm on Mar 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What you want to do is add those paragraph tags only where there is a complete blank line. Otherwise it will do precisely as you've said, add a paragraph for each line.

My ASP is too rusty to exemplify. You need a regexp that looks for a line that is only whitespace. Here's my PERL example. :-)

# ^ = beginning of any line
# $ = ending of any line
# \s+ = 1 or more of any white space character -
# if whitespace is all that's on a line, this includes any line feed
# s/a/b/; If you find a, substitute it for b

$line =~ s/^\s+$/<p>\n/g;

There's actually more to remove multiple lines and include a closing tag, but trying to K.I.S.S.

willybfriendly

6:05 pm on Mar 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



function nl2p($html)
{
// Use \n for newline on all systems
$html = preg_replace("/(\r\n¦\n¦\r)/", "\n", $html);

// Only allow two newlines in a row.
$html = preg_replace("/\n\n+/", "\n\n", $html);

// Put <p>..</p> around paragraphs
$html = preg_replace('/\n?(.+?)(\n\n¦\z)/s', "<p>$1</p>", $html);

// convert newlines not preceded by </p> to a <br /> tag
$html = preg_replace('¦(?<!</p> )\s*\n¦', "<br />", $html);

return $html;
}