I am trying to pass php array to ajax.
When I pass it into the onclick function, I get Uncaught SyntaxError: missing ) after argument list.
When I change only the variable $module the error dissapears.
PHP code :
<?php foreach($this->data AS $module_name => $module):
if ($module_name == "Descriptions") {
continue;
}
?>
<table>
<thead>
<tr>
<th onclick="loadModule('<?=$module_name?>', '<?=htmlspecialchars(json_encode($module))?>', '<?=json_encode($this)?>')" data-module="<?=$module_name?>" colspan="<?php echo count($this->langs) + 2; ?>"><?= $module_name; ?>
JS function loadModule:
function loadModule(module_name, module, thisObj) {
console.log("load");
if (!$(event.target).hasClass("btnEditDesc")) {
$.ajax({
url: "<?php echo linkGenerator::getIT()->buildURL('administration/webpage/loadSubmodules.php'); ?>",
type: "POST",
dataType: "html",
data: {
'thisObj': thisObj,
'module': module
},
beforeSend: function() {
console.log("before");
},
success: function(data) {
console.log(data);
// $("tbody['"+ module_name +"']").html(data);
$(this).parents("table").find("tbody").fadeToggle(200);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
}
});
}
}
var dump of the module array:
> array (size=2)
'names' =>
array (size=7)
'name' => string 'Names' (length=5)
'table_name' => string 'dictionary_words_names' (length=22)
'type' => int 2
'cols' => null
'key' => string 'dictionary_word_name_term' (length=25)
'col_prefix' => string 'dictionary_word_name_' (length=21)
'data' =>
array (size=7)
'en' => string 'The query to database has failed. Unknown column 'dictionary_word_name_en' in 'field list'' (length=90)
'cs' =>
array (size=275)
...
'de' =>
array (size=275)
...
'pl' =>
array (size=275)
...
'it' => string 'The query to database has failed. Unknown column 'dictionary_word_name_it' in 'field list'' (length=90)
'fr' => string 'The query to database has failed. Unknown column 'dictionary_word_name_fr' in 'field list'' (length=90)
'hu' => string 'The query to database has failed. Unknown column 'dictionary_word_name_hu' in 'field list'' (length=90)
'descriptions' =>
array (size=7)
'name' => string 'Descriptions' (length=12)
'table_name' => string 'dictionary_words_descriptions' (length=29)
'type' => int 2
'cols' => null
'key' => string 'dictionary_word_description_term' (length=32)
'col_prefix' => string 'dictionary_word_description_' (length=28)
'data' =>
array (size=7)
'en' => string 'The query to database has failed. Unknown column 'dictionary_word_description_en' in 'field list'' (length=97)
'cs' =>
array (size=275)
...
'de' =>
array (size=275)
...
'pl' =>
array (size=275)
...
'it' => string 'The query to database has failed. Unknown column 'dictionary_word_description_it' in 'field list'' (length=97)
'fr' => string 'The query to database has failed. Unknown column 'dictionary_word_description_fr' in 'field list'' (length=97)
'hu' => string 'The query to database has failed. Unknown column 'dictionary_word_description_hu' in 'field list'' (length=97)
I am not good at styling these questions, sorry about that.
htmlspecialchars(json_encode($module))? That will change the JSON string and possibly make it invalid JSON. If module needs it you shouldjson_encode(htmlspecialchars($module)).<?=htmlspecialchars(json_encode($module))?>and<?=json_encode($this)?>, when what your$modulecontains here is part of$thisalready? (You are looping over$this->datahere to fill$modulein the first place.) Why do you need this redudant information?foo(["bar", "baz"])andfoo('["bar", "baz"]')…?