0

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.

5
  • 2
    Why are you doing this? htmlspecialchars(json_encode($module))? That will change the JSON string and possibly make it invalid JSON. If module needs it you should json_encode(htmlspecialchars($module)). Commented Aug 13, 2020 at 12:53
  • 1
    If the error is happening client-side then the first thing you need to do is examine the client-side code, not the server-side code that generates the client-side code. Find what's wrong in the result of your code, that'll help you identify what you might need to change. Commented Aug 13, 2020 at 12:55
  • $module contains ", ' and other html tags and if I don't use htmlspecialchars and json_encode the array gets printed to the screen because it ends the onclick and th Commented Aug 13, 2020 at 12:57
  • What is the point of <?=htmlspecialchars(json_encode($module))?> and <?=json_encode($this)?>, when what your $module contains here is part of $this already? (You are looping over $this->data here to fill $module in the first place.) Why do you need this redudant information? Commented Aug 13, 2020 at 13:06
  • 2
    JavaScript level syntax question: Are you aware what the difference is between foo(["bar", "baz"]) and foo('["bar", "baz"]') …? Commented Aug 13, 2020 at 13:08

1 Answer 1

2

One approach could be to pass base 64 encoded string to the html instead of using htmlspecialchars. NOTE: you would also need to base 64 encode the 3rd parameter.

  <th onclick="loadModule('<?=$module_name?>', '<?=base64_encode(json_encode($module))?>', '<?=base64_encode(json_encode($this))?>')" data-module="<?=$module_name?>" colspan="<?php echo count($this->langs) + 2; ?>"><?= $module_name; ?>

And then in the javascript you can use atob to decode the base 64 before parsing the json.

  module = JSON.parse(atob(module));
  thisObj = JSON.parse(atob(thisObj));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.