...

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



atadon
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

e.g.


<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

glenngv
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:


<blah>
'blah'
"blah"

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

onclick="update_textarea('&lt;blah&gt;\n\'blah\'\n&quot;blah&quot;');"



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum