I’m trying to encode some user input I’m fetching from a database and then insert it into a textarea on click. Here’s my code for encoding the data, sending the user to the textarea anchor, and then triggering my function.
$quote = "[url]" . $quote . "[/url]";
$quote = htmlspecialchars($quote, ENT_QUOTES);
$quote = json_encode($quote, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
echo "
<a href='#quickpost' value='quote'
onClick=\"quote(" . $quote . ")\" name='quote'>Quote</a>
Here’s the quote() function:
function quote(originalpost){
oFormObject = document.forms['id1'];
oFormObject.elements['post'].value = originalpost;
};
I’m getting this error in firebug when I click it: SyntaxError: syntax error
Line 1
I’m a complete novice when it comes to JavaScript, so I could be missing something totally obvious.
I’ve looked and tried a lot of different encoding options and things for this, but I’m not getting anywhere.
Since JSON is native Javascript, you shouldn’t quote it when declaring it in JS code or passing it around.
Try:
Then inside your function, you may need to stringify the JSON:
For sanity’s sake, get a copy of the JSON produced by
json_encodeand run it through the JSON lint utility to make sure it is valid.See if that works. For browsers that don’t have native stringify support, you can get the code for the function here.