Formating a text area

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jbradly
    New Member
    • Mar 2008
    • 13

    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?
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    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.

    Comment

    • jbradly
      New Member
      • Mar 2008
      • 13

      #3
      Formating a text area

      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_exis ts("GetSQLValue String")) {
      function GetSQLValueStri ng($theValue, $theType, $theDefinedValu e = "", $theNotDefinedV alue = "")
      {
      $theValue = get_magic_quote s_gpc() ? stripslashes($t heValue) : $theValue;

      $theValue = function_exists ("mysql_real_es cape_string") ? mysql_real_esca pe_string($theV alue) : mysql_escape_st ring($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="baselin e">
      <td colspan="2" align="center" valign="middle" nowrap="nowrap" ><label>Announc ements</label></td>
      </tr>
      <tr valign="baselin e">
      <td nowrap="nowrap" align="right" valign="top"></td>
      <td><textarea name="Daily_Ann ouncements" cols="100" rows="15"></textarea> </td>
      </tr>
      <tr valign="baselin e">
      <td nowrap="nowrap" align="right">& nbsp;</td>
      <td><input type="submit" value="Add" /></td>
      </tr>
      </table>

      <input name="MM_insert 2" 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_Announce ments']; ?></span></td>
      </tr>
      <?php } while ($row_Recordset 1 = mysql_fetch_ass oc($Recordset1) ); ?>
      </table>[/HTML]
      Last edited by jbradly; May 7 '08, 03:58 PM. Reason: Add additional information

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        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?

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Hi.

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

          For example:
          [code=php]
          <?php
          if(isset($_POST['text'])) {
          echo "<pre>", $_POST['text'], "</pre>";
          }
          ?>

          <form action"?" method="post">
          <textarea name="text"></textarea><br />
          <input type="submit" />
          </form>
          [/code]

          Comment

          • jbradly
            New Member
            • Mar 2008
            • 13

            #6
            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_Even ts']; ?>[/PHP]
            and adding nl2br to it like this:

            [PHP]<?php echo nl2br($row_Reco rdset1['Important_Even ts']); ?>[/PHP]
            gets me the output that I want.
            It simple and it seems to work.

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              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.

              Comment

              • dlite922
                Recognized Expert Top Contributor
                • Dec 2007
                • 1586

                #8
                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_Even ts']; ?>[/PHP]
                and adding nl2br to it like this:

                [PHP]<?php echo nl2br($row_Reco rdset1['Important_Even ts']); ?>[/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.

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  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 ;)

                  Comment

                  • jbradly
                    New Member
                    • Mar 2008
                    • 13

                    #10
                    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?

                    Comment

                    • Markus
                      Recognized Expert Expert
                      • Jun 2007
                      • 6092

                      #11
                      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.

                      Comment

                      • jbradly
                        New Member
                        • Mar 2008
                        • 13

                        #12
                        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="baselin e">
                        <td colspan="2" align="center" valign="middle" nowrap="nowrap" ><label>Announc ements</label></td>
                        </tr>
                        <tr valign="baselin e">
                        <td nowrap="nowrap" align="right" valign="top"></td>
                        <td><textarea name="Daily_Ann ouncements" cols="100" rows="15"></textarea> </td>
                        </tr>
                        <tr valign="baselin e">
                        <td nowrap="nowrap" align="right">& nbsp;</td>
                        <td><input type="submit" value="Add" /></td>
                        </tr>
                        </table>

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

                        Comment

                        • dlite922
                          Recognized Expert Top Contributor
                          • Dec 2007
                          • 1586

                          #13
                          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.

                          Comment

                          • dlite922
                            Recognized Expert Top Contributor
                            • Dec 2007
                            • 1586

                            #14
                            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="baselin e">
                            <td colspan="2" align="center" valign="middle" nowrap="nowrap" ><label>Announc ements</label></td>
                            </tr>
                            <tr valign="baselin e">
                            <td nowrap="nowrap" align="right" valign="top"></td>
                            <td><textarea name="Daily_Ann ouncements" cols="100" rows="15"></textarea> </td>
                            </tr>
                            <tr valign="baselin e">
                            <td nowrap="nowrap" align="right">& nbsp;</td>
                            <td><input type="submit" value="Add" /></td>
                            </tr>
                            </table>

                            <input name="MM_insert 2" 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).

                            Comment

                            • Markus
                              Recognized Expert Expert
                              • Jun 2007
                              • 6092

                              #15
                              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.

                              Comment

                              Working...