I have a javascript function that has an array variable. I would like to get this array from PHP side. But I could not do it. Can anyone help me to how to get labels from the php side? I looked other questions but most questions are regarding to passing jquery to php. my one is reverse side.
$form_card.on('click', function(e) {
if(!$form_card.hasClass('card-expanded')) {
var template = Handlebars.compile($note_form_template),
context = {
'labels': [
{
'text': 'label 1',
'text_safe': 'label_1',
'type': 'default'
},
{
'text': 'label 2',
'text_safe': 'label_2',
'type': 'warning'
},
{
'text': 'label 3',
'text_safe': 'label_3',
'type': 'danger'
},
{
'text': 'label 4',
'val': 'label_4',
'type': 'success'
},
{
'text': 'label 5',
'text_safe': 'label_5',
'type': 'primary'
}
]
};
html = template(context);
$form.hide().html(html);
$note_form_text.hide();
Here is my ajax function...
$.ajax({
type: 'GET',
url: SITE_URL + 'nsys/dashboard/noteLabels',
//dataType: 'json',
success: function (result) {
var res = JSON.parse(result);
},
});
return res;
}
and this is my php side ...
public function noteLabels()
{
$labels = array(
0 => array(
'text' => 'label 1',
'text_safe' => 'label_1',
'type' => 'default'
),
1 => array(
'text' => 'label 2',
'text_safe' => 'label_2',
'type' => 'warning'
),
2 => array(
'text' => 'label 3',
'text_safe' => 'label_3',
'type' => 'danger'
),
3 => array(
'text' => 'label 4',
'val' => 'label_4',
'type' => 'success'
),
4 => array(
'text' => 'label 5',
'text_safe' => 'label_5',
'type' => 'primary'
)
);
echo json_encode($labels);
}