0

EDIT: Ok, thanks for your help guys. Using zerkms' suggestion, I have managed to get <br> tags inserted instead of carriage returns, so it is displayed properly on the page and doesn't break the javascript. The problem is now that when the user clicks on this text, it becomes a textarea and is therefore inline editable. However, the text string now has the tags displayed in it, rather than just having the editable text over multiple lines.

Any suggestions?

ORIGINAL QUESTION: I have a text area in my PHP application where users can enter notes in a project. Sometimes this is displayed on the page via PHP and sometimes it is displayed via javascript. The problem is, if the note is across multiple lines (i.e. the user presses enter while entering notes in the text area), it causes the JS to fail. It's fine when it's being done by the PHP.

The line of code in question is:

var editnotes='<textarea class="desc_text" style="width:20em;" id="notes_editor"><?php print $notes; ?></textarea>';

So, if the note is over multiple lines, the PHP builds the pager as:

var editnotes='<textarea class="desc_text" style="width:20em;" id="notes_editor">This is
a test note
over multiple lines
</textarea>';

And this obviously causes problems for the js. So my question is, what can I do to prevent this? As the code is being built by PHP before it even gets to the browser, I'm thinking that the best approach may be to parse it in the PHP so that the output is something more like this:

var editnotes='<textarea class="desc_text" style="width:20em;" id="notes_editor">This is<br/>a test note<br/>over multiple lines<br/></textarea>';

Will this work? How would I do it?

Thanks

2
  • How about fixing the part of the Javascript that "fails?" Commented Mar 31, 2010 at 3:10
  • Thanks, that would be the ideal solution. How do I get javascript to tolerate that variable being over multiple lines? Commented Mar 31, 2010 at 3:15

2 Answers 2

1

as of nl2br() didn't help second proposal is append \ to all line breaks. in some kind of manner:

preg_replace('!([\r\n]+)!', '\\\$1', $text);

to be clear, the sample:

var baz = 'foo
bar';

now become:

var baz = 'foo\
bar';
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, I'm not sure if that will work, as the text is coming from a MySQL text field, so I'm not sure the line breaks are stored in the string as "\n"
Ok so I tried using this, and it adds <br> to the string but does not remove the line break, so I end up with: a test note<br> over multiple lines<br>
oops, it's really not removed line breaks, sorry then :-(
CLARIFICATION: This doesn't seem to allow me to put comments over multiple lines, but the string starts in a new line after each <br>, as well as including the tag.
Thanks zerk, that didn't work for me either. I'd need the output to be more along the lines of var baz='foo<br>bar';. I've managed to get it to that stage now using ereg_replace, but the problem is that the html now doesn't render the line breaks in the textarea where the <br> tags are!
|
0

Use json_encode() to output a value for javascript.

var editnotes = <?php echo json_encode($str); ?>;

2 Comments

I've tried this, it just seems to put quotes around the text.
Are you suggesting that it doesn't produce a valid javascript string?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.