Sending array from PHP to javascript through ajax -
i have php code return many arrays javascript through ajax.
<?php ini_set('display_errors', 1); error_reporting(e_all); function get_events_data() { // load simplexml $nodes = new simplexmlelement('my_events.xml', null, true); $event_id = array(); $channel_id = array(); $channel_name = array(); $channel_onclick = array(); $event_site = array(); $event_url = array(); $start_date = array(); $start_time = array(); $end_date = array(); $end_time = array(); $event_notes = array(); $n = 0; foreach($nodes $node) { $event_id[$n] = $node['id']; $channel_id[$n] = $node->channel['id']; $channel_name[$n] = $node->channel->name; $channel_onclick[$n] = $node->channel->onclick; $event_site[$n] = $node->event_site->name; $event_url[$n] = $node->event_site->url; $start_date[$n] = $node->start_date; $start_time[$n] = $node->start_time; $end_date[$n] = $node->end_date; $end_time[$n] = $node->end_time; $event_notes[$n] = $node->notes; $n++; } $return['event_id'] = $event_id; $return['channel_id'] = $channel_id; $return['channel_name'] = $channel_name; $return['channel_onclick'] = $channel_onclick; $return['event_site'] = $event_site; $return['event_url'] = $event_url; $return['start_date'] = $start_date; $return['start_time'] = $start_time; $return['end_date'] = $end_date; $return['end_time'] = $end_time; $return['event_notes'] = $event_notes; echo json_encode($return); } echo get_events_data(); ?>
on javascript side have code access arrays.
$.ajax({ url: "get_events_data.php", type: "post", datatype : 'json', data: { }, cache: false, async: false, success: function (rdata) { var alert_data = 'event_id '+rdata.event_id[0]+'<br/>'+ 'channel_id '+rdata.channel_id[0]+'<br/>'+ 'channel_name '+rdata.channel_name[0]+'<br/>'+ 'channel_onclick '+rdata.channel_onclick[0]+'<br/>'+ 'event_site '+rdata.event_site[0]+'<br/>'+ 'event_url '+rdata.event_url[0]+'<br/>'+ 'start_date '+rdata.start_date[0]+'<br/>'+ 'start_time '+rdata.start_time[0]+'<br/>'+ 'end_date '+rdata.end_date[0]+'<br/>'+ 'end_time '+rdata.end_time[0]+'<br/>'+ 'event_notes '+rdata.event_notes[0]+'<br/>'; alert (alert_data); }, error: function (request, status, error) { alert ("status "+status+" error "+error+"responsetext "+request.responsetext); }, });
when print out first element of each array on javascript side, shows "object" doesn't show value in array. right way of accessing array data on javascript side?
in javascript, associative array pretty object. script working correctly. console.log()
data , explore how prints out in console, use access data. post json data in question , more detailed answers.
Comments
Post a Comment