Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Extra space and carriage returns in my textarea field.

Posted on 2008-11-01
8
871 Views
Last Modified: 2013-12-13
When I have my user submit some text in a textarea field on my form, I'm getting the text, but the first line is substantially indented and I invariably get some extra <BR> in the text. I have the code attached. Could someone please show me how to get rid of the indent and the extra line breaks.

Thanks!

Maybe it's a CSS issue, but I have my TEXTAREA set as

TEXTAREA {
      width:700px;
      height:250px;
      font-family: Verdana, Microsoft Sans Serif;
      font-size: 14px;
      color: #000000;
      }

So, I'm open to suggestions...
$text = $_POST['short_story'];
$textBr = nl2br($text);
$finaltext = addslashes($textBr);
 
$mainbody = $_POST['main_body'];
$textBody = nl2br($mainbody);
$bigstory = addslashes($textBody);
 
$article_date = date('Y-m-d', strtotime($_POST['article_date']) );
 
$insert = "insert into news (headline, short_story, article_date, main_body)
values ('$slash_headline', '$finaltext', '$article_date', '$big_story')";
$insertexe = mysqli_query($cxn, $insert)
or die ("Couldn't execute query.");

Open in new window

0
Comment
Question by:brucegust
  • 5
  • 2
8 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22857169
If it's only the first line, you can user the trim() function:
$text = trim($_POST['short_story']);
$mainbody = trim($_POST['main_body']);

Open in new window

0
 

Author Comment

by:brucegust
ID: 22857234
I did that and while it didn't correct the issue, I think it may have identified the problem.

I'm getting an extra line break, I think, in the UPDATE statement and I don't know how. Even after I do "trim," I still get a bit indent. But I'm wondering, now, if that indent that I see is actually a line break, but it's just showing up as an indent in the textarea field.

What do you think?
0
 
LVL 2

Expert Comment

by:vbsquickresponse
ID: 22857438
try with replace statement to replace <BR> with empty string or somthing like that
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22857476
What UPDATE statement? Your example has only an INSERT statement.

Where do you see the indent that you think might be a line break? In the textarea before the insert? Or in a different textarea after reading the text from the database?

If you are going to update the text using a textarea, you should not use  nl2br() when you INSERT the data. This function converts linefeeds to <br> html elements. You probably don't need those in the database. You should rather use the nl2br() function when you output the text from the database to a "normal" web page, i.e. not in the textarea.
0
 

Author Comment

by:brucegust
ID: 22857676
cxr - OK, I've eliminated the nl2br dynamic and you're right, it wasn't necessary and it was introducing additional breaks that I didn't need.

But even after cleaning it up, I still get a monster indent on the textarea that displays the text when I'm going to try and edit it.

Any ideas?
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22857709
Show us the code that creates the edit page, or at least the part that outputs the <textarea>
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 22858186
You probably have an error in the edit page. There should not be any spaces between the <textarea> tag and your variable. A single linefeed after the <textarea> tag is allowed, though:
# wrong
echo "<textarea>
      $message
      </textarea>";
 
# wrong
      <textarea>
      <?php echo $message; ?>
      </textarea>
 
# correct
echo "<textarea>$text</textarea>";
 
# correct
echo "<textarea>
$text
</textarea>";
 
# correct
      <textarea><?php echo $text; ?></textarea>
 
# correct
      <textarea>
<?php echo $text; ?>
      </textarea>

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22858226
... the last "correct" is not so good, it will insert spaces in the last line... the example should have been like this:

      <textarea>
<?php echo $text; ?>
</textarea>
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to check if a session is disconnected in php 9 32
How to resize a div in html 3 35
ASP.NET Content Page 3 25
issue with DB import 1 17
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question