Reputation: 5792
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.
Upvotes: 1
Views: 3677
Reputation: 67525
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.
Upvotes: 3