Solution: #PHP: Handling json_encode data in html data attribute

Updated September 2, 2015 ‚óŹ 3,397 views

If you are passing JSON encoded data to HTML data attribute, you need to properly escape the data and handle special characters.

Do the following when passing JSON encoded data in HTML data attribute:

<a href="javascript:void(0);" data-content="<?php echo htmlspecialchars(json_encode($comment), ENT_QUOTES, 'UTF-8'); ?>">TEST ACTION</a>



Our Terms of Use and Privacy Policy have changed.
By continuing to use this site, you are agreeing to the new Privacy Policy and Terms of Service.