0

i have two pages in my wordpress plugin

  • index.php
  • charts.php

in index.php i have some content and some link..

 <a onclick="show_trend()" >Trend</a>

while clicking this link ,i have an ajax function to create content in charts.php(i dont know if it is correct way)

function show_trend() {
 var data ="action=render_admin_charts_page";
   jQuery.ajax({
                    type: "POST",
                    url: "../wp-admin/admin-ajax.php",
                    data: data,
                    dataType: 'json',
                    success: function(data) {

                      alert(data);
                     //here i want to create html content

                    }
                });

}

andfollowing is my php function and hook

function render_admin_charts_page() {
    ?>
    <div class="wrap">
        <div class="chart">

        </div>
    </div>
    <?php
     die();
}
add_action( 'wp_ajax_render_admin_charts_page', 'render_admin_charts_page' );

now i am getting 0 as alert .

10
  • 3
    your data var should be an object: data = {action: 'render_admin_charts_page'}; Currently you just send a string (action=render_admin_charts_page), while WordPress ajax handler is looking for $_REQUEST['action'], which is not set in your case. See line 26 in wp-admin/admin-ajax.php Commented Jul 3, 2014 at 7:51
  • @ungestaltbar can u elaborate and post as an answer? Commented Jul 3, 2014 at 7:58
  • Where did you define your render_admin_charts_page function?
    – sri
    Commented Jul 3, 2014 at 9:09
  • in charts.php . Commented Jul 3, 2014 at 9:11
  • 1
    @sri as ungestaltbar said i hv changed to object Commented Jul 3, 2014 at 9:16

2 Answers 2

1

I see two things. You're missing an action call - you need

add_action( 'wp_ajax_render_admin_charts_page', 'render_admin_charts_page' );
add_action('wp_ajax_nopriv_render_admin_charts_page', 'render_admin_charts_page');

Plus, from looking at your render_admin_charts_page() you need to change your json dataType to html. Your charts function is outputting html so all you need to do is output it; your alert() should contain all the html from your function.

One more thing, if this is still a live question, why die() in your render_admin_charts_page() function? That function prints a message so that could also be messing with your returned value.

1
  • Thanks for the edits, kaiser. This is a lot easier to read. Still getting the hang of this
    – dg4220
    Commented Apr 8, 2016 at 2:33
0

As ungestaltbar said, the variable "data" should contain an object, not a string.

function show_trend() {
   var data = {
       action: "render_admin_charts_page"
   }
   jQuery.ajax({
                    type: "POST",
                    url: "../wp-admin/admin-ajax.php",
                    data: data,
                    dataType: 'json',
                    success: function(data) {

                      alert(data);
                     //here i want to create html content

                    }
                });

}

That should do it.

1
  • this chage makes nothing! Commented Jul 3, 2014 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.