Tuesday, 15 March 2011

php - AJAX in Wordpress - loading some data in 2 different divs. -



php - AJAX in Wordpress - loading some data in 2 different divs. -

i utilize next jquery code retrieve post content via ajax in wordpress:

$.ajax({ type: 'post', url: ajax_object.ajaxurl, data: ({action : 'ajax_page', post_id: page_id }) }) .done(function(data){ $('#page-content').html(data); });

the php function:

function ajax_page() { $post_id = $_post['post_id']; $post_data = get_post($post_id); $title = $post_data->post_title; $content = $post_data->post_content; echo $title; echo $content; die(); // remove trailing 0 }

but able load $title , $content in 2 different divs.

$.ajax({ type: 'post', url: ajax_object.ajaxurl, data: ({action : 'ajax_page', post_id: page_id }) }) .done(function(data){ $('#page-title').html(title); $('#page-content').html(data); });

many time , help.

you can utilize array seperate ajax content in clean way :

function ajax_page() { $post_id = $_post['post_id']; $post_data = get_post($post_id); $title = $post_data->post_title; $content = $post_data->post_content; echo json_encode(array($title,$content)); die(); // remove trailing 0 }

and 2 distinct homecoming values array :

.done(function(data){ var mydata = jquery.parsejson(data); $('#page-title').html(mydata[0]); $('#page-content').html(mydata[1]); });

php jquery ajax wordpress

No comments:

Post a Comment