JSON Vs. PHP HTMLEntities and the headache it can cause


So my programming pet project at the moment is a CRM-ish type application. I am writing it using primarily JavaScript/Jquery and PHP. I have never really been a huge fan of javascript, but if you want a dynamic front end with PHP power you need run PHP via ajax. So now comes the headache part. One of the problems with trying to do this is how to effectively pass information back and forth between client and server side. Enter Json…

The way I have my code setup is simple, but very effective. My user page submits a post ajax requests to a php file. The PHP then returns it’s answer via a Json. There is a little more to it, but that is the gist of it it. So why the gruesome title you ask? Well after writing¬† code this morning I noticed that some of the fields on that should be populated by ajax just showed up completely blank. OK, no big deal I can handle that. So I went and looked at the traffic back and forth from my PHP AJAX file and notice something. Sometimes instead of returning a json string it just return null. That is never good, but I’m still not worried. Since I just want a quick answer I drop in a die and var_dump to see what is going on. I am the only one working on it and I want to know what is wrong now. Sure enough the data is good and var_dump confirms it is all their prior to being feed into json_encode.

Concern starts to set in at this point. The information is there, it is getting feed into json_encode(), but then…. FAIL! It spits out null. So at this point I jump on the net and have a look to see who else has this issues. Turns out this is normally a UTF8 encoding problem. So at this point I start to get real worried. I made real sure when I was setting everything up to keep all my encoding consistent. I even went as far as to make sure I had used some tricks to make sure that even on old php versions PDO connects using UTF-8.

So of course I freak and check my DB connection string, I double check all my code and spent about a half hour trying to figure it out. Finally I try to feed from the DB into json_encode. Everything works fine and the data displays. Then I start to look back at what I was using to prep data for display.

It turns out that at some point I used htmlentities without the second and third parameters. Uh Oh, just made a noob move. Turns out that when the right character set goes into htmlentities without that second and third parameter what comes out can’t be encoded by json_encode. What was happening was that I was putting in UTF8 and not getting UTF8 back out. The fix is pretty simple, in fact it made me laugh. Don’t use htmlentities without the second and third parameters. Better yet just use htmlspecialchars($YourString, ENT_QUOTES, “UTF-8”)

Leave a Reply