0

I have looked over many posts on here and other websites and nothing I do seems to work.

What I am doing is, I have a custom template file and inside that file I have a div that I want to populate using jquery ajax. The problem is that the response is always 0.

Note: I have used this code before in a plugin and not a template file and it worked the way it was supposed to. This makes me wonder if the add_action code isn't working right.

<script type="text/javascript">
jQuery(document).ready(function() {
jQuery.ajax({
    type : "post",
    dataType : "html",
    url : "<?php echo site_url(); ?>/wp-admin/admin-ajax.php",
    data : {action : "tw_pages_results", 'page':0, 'itemsperpage':<?php echo $item_per_page; ?>, nonce: "<?php echo $tw_pages_nonce; ?>"},
    success: function(response) {
        jQuery("#results").html(response);
        jQuery("#1-page").addClass('active');
    }
});
});
</script>

tw_page.php

This is just the beginning part of the function in the file loaded by the ajax.

add_action("wp_ajax_tw_pages_results", "tw_pages_results");
add_action("wp_ajax_nopriv_tw_pages_results", "tw_pages_results");

function tw_pages_results() {
    if ( !wp_verify_nonce( $_POST['nonce'], "tw_pages_nonce")) {
      exit("No naughty business please");
    }   

tw_pages_results.php

  • please ass the code to the question itself – Mark Kaplun Sep 4 '14 at 2:29
  • Im not sure what you mean by ass the code but I added the some code to my post. – Tommy Wilkey Sep 4 '14 at 4:05
  • lol sorry, meant add – Mark Kaplun Sep 4 '14 at 4:32
  • First check in Firebug that all data parameters are passed in the AJAX request. Another thing is that, echo and exit with print dummy text outside condition. Check whether it is coming or not. – Nilambar Sep 4 '14 at 4:40
  • All the data is being posted according to Firebug. The problem seems to be that the tw_pages_results function isn't being ran. I added some notes to my original post. – Tommy Wilkey Sep 4 '14 at 5:42
1

Add die(); to the end of the function handle ajax. Add return false; after jQuery.ajax

function tw_pages_results() {
    //stuff
    die(); }

jQuery(document).ready(function() {
jQuery.ajax({
    type : "post",
    dataType : "html",
    url : "<?php echo site_url(); ?>/wp-admin/admin-ajax.php",
    data : {action : "tw_pages_results", 'page':0, 'itemsperpage':<?php echo $item_per_page; ?>, nonce: "<?php echo $tw_pages_nonce; ?>"},
    success: function(response) {
        jQuery("#results").html(response);
        jQuery("#1-page").addClass('active');
    }
});
return false;
});
0

For anyone that may come across this issue, my problem was that I included the tw_pages_results.php file inside the template file. This will not work. So I moved my include to my function.php file and it started working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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