1

I have loop in PHP with javascript: First array is printing on console output. But, in second element of array I am getting this error:

ReferenceError: array is not defined

<?php foreach($job_requirements_names as $jrn){ ?>
    <div class="col-md-12 form-group">
            <label class="control-label label-top" for="requirement_<?=strtolower($jrn['name'])?>"><?=$jrn['name']?></label>
            <?php
               $query = new QUERY(array('TABLE'=>$table_name, 'KEY'=>array('name'=>$jrn['name']), 'ASC'=>'n_option'));
               $options = $query->fetchAll();
               unset($query);
               $id = "requirement_".strtolower($jrn['name']);
            ?>
            <script>
               // For countries:
               var data = '<?php echo json_encode($options);?>';
               console.log(data);
               data = JSON.parse(data);
               data = data.map(function (v) { return {id: v.id, text: v.n_option}; });

               $("#<?=$id?>").select2({
                  multiple: true,
                  data: data
               }).select2('data', array());
            </script>

            <input id='requirement_<?=strtolower($jrn['name'])?>' name="requirement_<?=strtolower($jrn['name'])?>" class="col-md-12"/>
    </div>
<?php } ?>

As you can see in javascript array is already defined.

4
  • 4
    Use [] instead of array(). Commented Oct 14, 2016 at 16:04
  • So do you have a array() function in javascript? Commented Oct 14, 2016 at 16:04
  • select2('data', array()) to select2('data', []) was meant Commented Oct 14, 2016 at 16:09
  • array() is a function call, and you don't have a function named array. Commented Oct 14, 2016 at 16:11

1 Answer 1

3

You should to use Array() instead of array() (Not exist) since JS is a case sensitive language, or simply use [], so it will be :

.select2('data', new Array());
//OR
.select2('data', []);

Hope this helps.

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.