Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem - TEXTAREA not working exactly right 2

Status
Not open for further replies.

JustKIDn

MIS
May 6, 2002
386
US
In one of my forms I use;

Code:
<textarea name="ud_notes" rows=3 cols=33><? print "$notes"; ?></textarea>

It works fine except, if it's an existing record, when you enter into the field, the cursor lands about 60 or 70 charactor spaces in from the beginning.

How do I get it to start at the beginning of the first line?

ps. I was going to ask this in the HTML forum. But I figured someone would see the php code and suggest I ask the question in the php forum.

____________________________
JustKIDn

____________________________
Families can be together forever...
 
Why do you encapsul the variable in quotes?
Quotes are for strings!

Code:
<textarea name="ud_notes" rows="3" cols="33"><?=$notes?></textarea>

I dont know if this will make any difference, but, are you sure that the $notes does not contain any \n?

You can try this:
Code:
<textarea name="ud_notes" rows="3" cols="33"><?php echo nl2br($notes); ?></textarea>


Olav Alexander Mjelde
Admin & Webmaster
 
You might look at javascript to set the focus on the field you want
 
DaButcher,

Thanks for your input.

I don't know why I put the variable in quotes. I think I did that in another piece of code with javascript, and had to put it in quotes to make all the syntax work. Then I just copy/pasted it somewhere else and then again, and again, etc... you get the idea.

Anyway it works.

I don't currently store notes for every record, so the notes field is actually in a separate table. The record holding notes shouldn't get created unless something has been entered into the notes field.

This is where the problem comes in. If I modify the record and submit it, even if I don't enter anything into the notes field, a record gets created for the notes because there is a string of spaces (or tabs) there.

The only way I can prevent it from creating a record, is to drop into the notes box and backspace until all the spaces/tabs are gone. Then when I hit submit, everything is saved as it should be and no record is created for the notes since I haven't entered any.

On the other hand, if I choose to add a new record in the main table. There are no spaces or tabs in the notes field. And if I save it without adding anything into the notes field. No record for the notes is created. (as it should be)

So how do I get the textarea to stay empty until I enter something?

As I write this, it occurs to me that it's the way I'm populating the variable.
I will try something different and see if that helps.

____________________________
JustKIDn

____________________________
Families can be together forever...
 
I think your problem might be in your update query.

even so, please read up on this:

it will trim away whitespaces at beginning and end of a string.

I hope this is what you are looking for.

Good luck and good night.

Olav Alexander Mjelde
Admin & Webmaster
 
Thanks for the help!

It's fixed. It turns out, it's not always good to code neatly.

In my code, I had;

<textarea name="ud_notes" rows=3 cols=33><? print "$notes"; ?>
</textarea>

As I do with many items like this, if the line is long, I like to have the closing tag on the next line so it's neat and easy to read.
However, textarea takes everything between the opening and closing tags as default text. Hence, all the tabs.

So it was my fault.

So long and thanks for all the fish!

____________________________
JustKIDn

____________________________
Families can be together forever...
 
yes, but still:
use the trim() on the $variable that will be inserted in the database! Then it will remove tabs, whitespace, etc.

Also, use strip_tags(), with optional tags, if wanted.
strip_tags will remove html, javascript, etc. but you can specify tags that are allowed, like this:

$variable = strip_tags(trim($variable), '<b><p><img>')

Olav Alexander Mjelde
Admin & Webmaster
 
That's a good idea to add trim().

Another question. This is related.

If I enter some text in the TEXTAREA and press enter and add more text. The next time I display the text in a TEXTAREA box. It displays correctly, complete with newlines/carraige returns etc.

On the other hand, if I display the field using print (or echo) instead of TEXTAREA. The text displays, but I lose any formatting.

Do you know of a way to keep the formatting when using print?

____________________________
JustKIDn

____________________________
Families can be together forever...
 
It is not print at fault here rather than the way html handles white-space: repeated space, tab characters, carriage returns. All these are ignored in regular html making them a single space character. The easiest way is to encapsulate all your text in <pre></pre> tags, which preserve formatting similar to textareas. If it is only new lines you're after, nl2br will convert normal new lines to html breaks.
 
Vragabond,

nl2br() was the answer I needed.

I know you should be careful with nl2br because if HTML tags are inserted into the textarea, problems could arise.

For this application, I won't have to worry about that too much. I will be the primary user and there are 3 others and they know nothing about html.

[frosty]

Thanks for all the help!

____________________________
JustKIDn

____________________________
Families can be together forever...
 
You're welcome,

Thank you for the help.

Without the help, there would be no stars!

[roll2][&nbsp;][&nbsp;] [roll1][&nbsp;][&nbsp;] [spin][&nbsp;][&nbsp;] [spin2][&nbsp;][&nbsp;] [spin][&nbsp;][&nbsp;] [roll1][&nbsp;][&nbsp;] [roll2]

____________________________
JustKIDn

____________________________
Families can be together forever...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top