[RFE] Example code improvement (?) suggestion / bugfix

[RFE] Example code improvement (?) suggestion / bugfix

MorreMorre Posts: 4Questions: 0Answers: 0
edited January 2011 in General
Hello,

I believe the server-side processing example listed at http://datatables.net/examples/server_side/server_side.html contains an error in the code.

The addslashes() calls around each entry in the $sOutput string will escape single quotes ( ' ) into escaped quotes ( \' ), but these will not be properly handled by the JSON parser and will thus give an error. This can be fixed either by using str_replace to replace any occurrences of escaped quotes with single quotes after $sOutput has been finalized, or possibly by using PHP's addcslashes() function to escape specific symbols (I haven't tested this latter approach, but I believe it might be cleaner).

Another option might be to use PHP's built-in json_encode function.

Replies

  • GregPGregP Posts: 500Questions: 10Answers: 0
    That code is working for me (and I assume working for the live demo).

    Or do you mean to say that if your data itself has a single quote in it? I have to admit, my data set does not, so I'm not sure if it would fail or not.
  • MorreMorre Posts: 4Questions: 0Answers: 0
    GregP - Yes, the data itself (in the database) has single quotes in it. This then gets escaped with addslashes, which in turn creates JSON that cannot be parsed by the jQuery parser.
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Urgh sorry - the visible example code on that page is out of date... The script it is actually using is here: http://www.datatables.net/development/server-side/php_mysql - which does use json_encode. I've updated the example now.

    Allan
This discussion has been closed.