View Full Version : Unhappy passing/escaping javascript function parameters to update a textarea

08-05-2006, 04:00 AM
hi guys

here is what i want to achieve:

i have e.g. a piece of text that i grabbed from a db, e.g:

LimSee2 is an open source SMIL 2.0 authoring tool.
"@% #$^ $& ^* &) (*_() + )(_)(&* ^^% $ @~!@ {}" ":">?< >?<,./,.,

$sometext = "LimSee2 blablabla (same as above)";

now i want to update a textarea with $sometext from an anchor element by calling a javascript function


<a href="#" title="update textarea" onclick="update_textarea('<?php echo $sometext ?>')">update</a>

the javascript function is very simple:

function update_textarea(sometext) {
document.getElementById('sometextarea').value = sometext;

but bekoz of the new lines and symbols, the parameters get screwed up and the function can't be called.

i know of functions such as htmlentities, htmlspecialchars, addslashes, etc etc but in the end, i get confused.

can some1 plz post the right steps to take in order to pass the parameters correctly?

thanks a lot for reading this

// cheers

08-06-2006, 01:36 AM
I dont know PHP but here's my idea on what to do.

1. Replace "\n" with "\\n".
2. Replace special characters with HTML entities using htmlentities function converting double qoutes but leaving single quotes alone. (ENT_COMPAT quote style)
3. Replace "'" with "\'".

So if the text looks like this:


the final output in the HTML source should be like this: