Question

Surprisingly I found near to nothing on this topic but I think there´s a very simple solution.

Problem description:

I have a Javascript variable which is filled with a PHP variable:

var createdEncode = '<?php echo $createdEncode; ?>';

The PHP contains a JSON-string but without the starting and ending curly bracket. I checked for delimiter-problems but that string is fine how I think, see for yourself:

var createdEncode = '"sEcho":1,"iTotalRecords":15,"iTotalDisplayRecords":11,"aaData":[["1","Suche Kurier","","dauerhaft, immer","","","ein geschenk","3","test dauerhaft","0","1","1","",""],["4","Suche Hilfe","1","bis 09.01.2014, sdfkdsjf","","","Du gibst:  9\u20ac","1","Auch gibt es niemanden, der den Schmerz an sich liebt, sucht ...","1","1","0","",""],["5","Suche Kurier","","bis 15.01.2014, jklkl","","","Du gibst:  8\u20ac","1","jllk","1","1","0","",""],["7","Suche Kurier","","bis 20.01.2014, jkljkl","","","Du gibst:  8\u20ac","1","nm,.","0","1","0","",""],["8","Suche Hilfe","1","am 25.01.2014 um 08:45 Uhr","","","Du gibst:  8\u20ac\/h","2","dsdf","0","1","0","",""],["9","Biete Kurier","","bis 08.01.2014, swrslkjk","","","kljkjl","3","Auto Kompaktklasse, H\u00e4lfte des Kofferraums freier Platz","1","1","0","2","4"],["10","Biete Hilfe","1","am 26.01.2014 um 12:45 Uhr","","","Du bekommst:  6\u20ac","1","sdsdfs","0","1","0","",""],["11","Suche Kurier","","bis 23.01.2014, sfui","","","Du gibst:  7\u20ac","1","jlkkjl","0","1","0","",""],["12","Suche Hilfe","1","am 15.01.2014 um 13:00 Uhr","","","Du gibst:  8\u20ac\/h","2","sdfkl","1","1","0","",""],["13","Biete Kurier","","bis 29.01.2014, erert","","","Du bekommst:  4\u20ac\/h","2","Zu Fu\u00df, Dreiviertel des Kofferraums freier Platz","0","1","0","0","5"],["14","Biete Hilfe","1","dauerhaft, dfgdf","","","Du bekommst:  3\u20ac\/h","2","xfsfds\u00f6k","0","1","0","",""]],"oLanguage":{"sUrl":"language\/dataTables.german.txt"}';

Later on I got some JS which says:

$('#postsCreatedData').dataTable({
    createdEncode,
    "bAutoWidth": false,
    "aoColumnDefs":[
        {"aTargets .... stuff ...

My problem is that "createdEncode" does nothing, the console throws Uncaught SyntaxError: Unexpected token ,. Meant is the comma after "createdEncode".

My approaches:

I tried it with document.write(createdEncode); It throws unexpected token .. Also tried + createdEncode +. Same thing here, unexpected + ... I really don´t know what to do. Can you offer me a solution to print the contents of that variable into that datatable?

Thanks a lot.

Solution:

Since Javascript needs a proper object rather than a string to work as intended I had to rewrite the complete datatable()-call. Thanks to MueR for getting me on the right track.

Basically I transformed the printed PHP variable into a standard JSON-object and extended it with the other options which I set to a variable as a JSON-object too.

All I did then is:

var createdParams = $.extend({}, createdEncode, appendix); which plugs the "appendix"-JSON to the createdEncode, so the call now works with:

$('#postsCreatedData').dataTable(createdParams);

Was it helpful?

Solution

Your problem is that you're putting a string (without key) in that object, which is invalid. Your resulting javascript is

{
    "a string without index, which is illegal",
    "bAutoWidth": false,
        "aoColumnDefs":[]
}

To correctly do it, you would need to have PHP also include the brackets, not the surrounding single quotes, so it's an actual object. Then you would have to merge the createdEncode with the other object containing bAutoWidth etc.

You could try this (assuming you'll let PHP output an actual javascript object)

var dataTableParams = $.extend({}, createdEncode, {"bAutoWidth": false, .. other things ..});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top