Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Unhappy passing/escaping javascript function parameters to update a textarea

    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.
    "@% #$^ $& ^* &) (*_() + )(_)(&* ^^% $ @~!@ {}" ":">?< >?<,./,.,


    PHP Code:

     $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.

    PHP Code:
    <a href="#" title="update textarea" onclick="update_textarea('<?php echo $sometext ?>')">update</a>
    the javascript function is very simple:

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

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    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:
    Code:
    <blah>
    'blah'
    "blah"
    the final output in the HTML source should be like this:
    Code:
    onclick="update_textarea('&lt;blah&gt;\n\'blah\'\n&quot;blah&quot;');"


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •