Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old May 7th, 2008, 01:13 PM
Newbie
 
Join Date: Mar 2008
Posts: 13
Default Formating a text area

I have a form with only one element (a text area created in Dreamweaver.) I want a user to input text in the form in any way he wants(such as paragraphs) and have it output pretty much the same way it was entered except on a different web page. I read about the function nl2br() but I do not understand how it works. I don't need anything as sophisticated as this message window I used to type in the message, I just want my user to be able to type in a textarea and if he wants a new paragraph

then all he would have to do

is hit the enter button

and the text would be displayed like you are seeing it now. I want to be able to do something similiar.

Any suggestions?
Reply
  #2  
Old May 7th, 2008, 01:36 PM
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Age: 18
Posts: 2,860
Default

Quote:
Originally Posted by jbradly
I have a form with only one element (a text area created in Dreamweaver.) I want a user to input text in the form in any way he wants(such as paragraphs) and have it output pretty much the same way it was entered except on a different web page. I read about the function nl2br() but I do not understand how it works. I don't need anything as sophisticated as this message window I used to type in the message, I just want my user to be able to type in a textarea and if he wants a new paragraph

then all he would have to do

is hit the enter button

and the text would be displayed like you are seeing it now. I want to be able to do something similiar.

Any suggestions?
nl2br() sounds like the correct thing to use.
What it does is convert newlines(nl) into(2) linebreaks(br) - smart, eh?
So, when a user types something such as
"hello
this
is
a
new
line"
it converts those new lines into <br /> tags, thus keeping the formatting the user gave.
Reply
  #3  
Old May 7th, 2008, 04:29 PM
Newbie
 
Join Date: Mar 2008
Posts: 13
Default Formating a text area

Quote:
Originally Posted by markusn00b
nl2br() sounds like the correct thing to use.
What it does is convert newlines(nl) into(2) linebreaks(br) - smart, eh?
So, when a user types something such as
"hello
this
is
a
new
line"
it converts those new lines into <br /> tags, thus keeping the formatting the user gave.

That makes sense.
Now where and how to put it in the code? Does it go on the form page or the result page (the page I use to display the text)?

On the form page, I have the following:
Here is a part of php code that Dreamweaver put in for stripslashes and htmlentities.
[PHP]
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);[

}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}


[/PHP]
Then after the php ends, the html code begins.
Here is my form code,
[HTML]<form action="<?php echo $editFormAction; ?>" method="POST" name="form2" id="form2">
<table align="center">
<tr valign="baseline">
<td colspan="2" align="center" valign="middle" nowrap="nowrap"><label>Announcements</label></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right" valign="top"></td>
<td><textarea name="Daily_Announcements" cols="100" rows="15"></textarea> </td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">&nbsp;</td>
<td><input type="submit" value="Add" /></td>
</tr>
</table>

<input name="MM_insert2" type="hidden" value="form2" />
</form>[/HTML]

On the result page, I have the same php code as above plus this code that I use to display the text.
[HTML]table width="457" height="170" border="1">
<tr>
<td><div align="center" class="style1">Daily Announcements</div></td>
</tr>
<?php do { ?>
<tr>
<td><span class="style2"><?php echo $row_Recordset1['Daily_Announcements']; ?></span></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>[/HTML]

Last edited by jbradly; May 7th, 2008 at 04:58 PM. Reason: Add additional information
Reply
  #4  
Old May 7th, 2008, 10:05 PM
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Age: 18
Posts: 2,860
Default

if the data is being saved into a database, then you should format the text before it is inserted - otherwise you'll be using unecessary resources when formatting the data being retrieved from the DB.

So, are you inserting into a database?
Reply
  #5  
Old May 8th, 2008, 12:46 AM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Age: 22
Posts: 2,790
Default

Hi.

Try using the <pre> tags when displaying the text from the text-area.

For example:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if(isset($_POST['text'])) {
  3.   echo "<pre>", $_POST['text'], "</pre>";
  4. }
  5. ?>
  6.  
  7. <form action"?" method="post">
  8.   <textarea name="text"></textarea><br />
  9.   <input type="submit" />
  10. </form>
  11.  
Reply
  #6  
Old May 8th, 2008, 09:28 PM
Newbie
 
Join Date: Mar 2008
Posts: 13
Default

Quote:
Originally Posted by markusn00b
if the data is being saved into a database, then you should format the text before it is inserted - otherwise you'll be using unecessary resources when formatting the data being retrieved from the DB.

So, are you inserting into a database?
Yes. The data is going into a database. I did create a solution that seems to work although I do not know if it is the best way. My user would put in his text with whatever line breaks he wants and submit it to the database. I have a seperate page to display the data and it has this code:
[PHP]<?php echo $row_Recordset1['Important_Events']; ?>[/PHP]
and adding nl2br to it like this:

[PHP]<?php echo nl2br($row_Recordset1['Important_Events']); ?>[/PHP]
gets me the output that I want.
It simple and it seems to work.
Reply
  #7  
Old May 9th, 2008, 07:48 AM
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Age: 18
Posts: 2,860
Default

Because you're doing the formatting 'on the fly' you take up more resources - if you were to save a formatted version of the text, you'd only need to nl2br() it once (instead of each loading from the database).

Regards.
Reply
  #8  
Old May 9th, 2008, 07:59 AM
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Age: 23
Posts: 673
Default

Quote:
Originally Posted by jbradly
Yes. The data is going into a database. I did create a solution that seems to work although I do not know if it is the best way. My user would put in his text with whatever line breaks he wants and submit it to the database. I have a seperate page to display the data and it has this code:
[PHP]<?php echo $row_Recordset1['Important_Events']; ?>[/PHP]
and adding nl2br to it like this:

[PHP]<?php echo nl2br($row_Recordset1['Important_Events']); ?>[/PHP]
gets me the output that I want.
It simple and it seems to work.
if you want advanced formatting check out TinyMCE and other WYSIWYG editors.

They usually output HTML code, which you can save in the database, kinda like MySpace.
Reply
  #9  
Old May 9th, 2008, 08:09 AM
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Age: 18
Posts: 2,860
Default

Quote:
Originally Posted by dlite922
if you want advanced formatting check out TinyMCE and other WYSIWYG editors.

They usually output HTML code, which you can save in the database, kinda like MySpace.
OR, indeed, like the editor you use to post on here ;)
Reply
  #10  
Old May 9th, 2008, 07:39 PM
Newbie
 
Join Date: Mar 2008
Posts: 13
Default

Quote:
Originally Posted by markusn00b
OR, indeed, like the editor you use to post on here ;)
An editor like the one used here would be great but here is my problem. I do not have access to the server, In order to change or setup a table in the database, I have to go through the server administrator. Does TinyMCE require installation on a server?

Back to using resouces, where would I put in the nl2br tag to format the text before it goes to the database?
Reply
  #11  
Old May 9th, 2008, 07:56 PM
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Age: 18
Posts: 2,860
Default

Quote:
Originally Posted by jbradly
An editor like the one used here would be great but here is my problem. I do not have access to the server, In order to change or setup a table in the database, I have to go through the server administrator. Does TinyMCE require installation on a server?

Back to using resouces, where would I put in the nl2br tag to format the text before it goes to the database?
It does require ftp'ing.

Well, we'd have to see the code you use to insert the data into the db.
Reply
  #12  
Old May 10th, 2008, 02:36 AM
Newbie
 
Join Date: Mar 2008
Posts: 13
Default

Quote:
Originally Posted by markusn00b
It does require ftp'ing.

Well, we'd have to see the code you use to insert the data into the db.
This is the form code
[HTML]<form action="<?php echo $editFormAction; ?>" method="POST" name="form2" id="form2">
<table align="center">
<tr valign="baseline">
<td colspan="2" align="center" valign="middle" nowrap="nowrap"><label>Announcements</label></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right" valign="top"></td>
<td><textarea name="Daily_Announcements" cols="100" rows="15"></textarea> </td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">&nbsp;</td>
<td><input type="submit" value="Add" /></td>
</tr>
</table>

<input name="MM_insert2" type="hidden" value="form2" />
</form>[/HTML]
Reply
  #13  
Old May 10th, 2008, 07:25 AM
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Age: 23
Posts: 673
Default

Quote:
Originally Posted by jbradly
An editor like the one used here would be great but here is my problem. I do not have access to the server, In order to change or setup a table in the database, I have to go through the server administrator. Does TinyMCE require installation on a server?

Back to using resouces, where would I put in the nl2br tag to format the text before it goes to the database?
TinyMCE does not require any installation at all.

It is javascript base and just need to upload a couple of javascript files at the most.

I've tested the top 5 editors, and TinyMCE is the most flexible, the stable bug free, and easy to use editor out of all of them. Even the java-applet based ones.

You can even "create" your own items on the menu and offers thousands of configuration options. If you can't customize TinyMCE to your needs no matter how small or how large, you can't do it anywhere in my opinion.

Good luck.
Reply
  #14  
Old May 10th, 2008, 07:27 AM
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Age: 23
Posts: 673
Default

Quote:
Originally Posted by jbradly
This is the form code
[HTML]<form action="<?php echo $editFormAction; ?>" method="POST" name="form2" id="form2">
<table align="center">
<tr valign="baseline">
<td colspan="2" align="center" valign="middle" nowrap="nowrap"><label>Announcements</label></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right" valign="top"></td>
<td><textarea name="Daily_Announcements" cols="100" rows="15"></textarea> </td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">&nbsp;</td>
<td><input type="submit" value="Add" /></td>
</tr>
</table>

<input name="MM_insert2" type="hidden" value="form2" />
</form>[/HTML]
This is not the code that inserts it into the DB. This is the code used to display the form (textareas).
Reply
  #15  
Old May 10th, 2008, 10:28 AM
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Age: 18
Posts: 2,860
Default

Quote:
Originally Posted by dlite922
TinyMCE does not require any installation at all.

It is javascript base and just need to upload a couple of javascript files at the most.

I've tested the top 5 editors, and TinyMCE is the most flexible, the stable bug free, and easy to use editor out of all of them. Even the java-applet based ones.

You can even "create" your own items on the menu and offers thousands of configuration options. If you can't customize TinyMCE to your needs no matter how small or how large, you can't do it anywhere in my opinion.

Good luck.
It would still require to be ftp'd. Which would need access to the server.
Reply
  #16  
Old May 11th, 2008, 05:44 AM
Newbie
 
Join Date: Mar 2008
Posts: 13
Default

Quote:
Originally Posted by dlite922
This is not the code that inserts it into the DB. This is the code used to display the form (textareas).
Sorry
Here is everything I have of code to insert into the DB
[PHP]<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form2")) {
$insertSQL = sprintf("INSERT INTO announcements (ID) VALUES (%s)",
GetSQLValueString($_POST['Principal_Message'], "int"));

mysql_select_db($database_production, $production);
$Result1 = mysql_query($insertSQL, $production) or die(mysql_error());

$insertGoTo = "view_announcement.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form2")) {
$insertSQL = sprintf("INSERT INTO announcements (Principal_Message) VALUES (%s)",
GetSQLValueString($_POST['Principal_Message'], "text"));

mysql_select_db($database_production, $production);
$Result1 = mysql_query($insertSQL, $production) or die(mysql_error());

$insertGoTo = "result.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_production, $production);
$query_Recordset1 = "SELECT ID, Principal_Message FROM announcements";
$Recordset1 = mysql_query($query_Recordset1, $production) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>[/PHP]

Also, I misspoke earlier. I can ftp files to the server, what I cannot do is install anything to the root directory to the server. I have only ftp access to a sub-directory on the server. However I can use php and mysql to post data to tables in a database. But, I have to have the admin set up the db and tables.
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles