I am focusing on a wordpress project and getting an irritating problem with ajax.

I am trying to setup an ajax call from the wordpress page to some php script I've known as getHistoricalTransFunctions.php. getHistoricalTransFunctions.php then features a file filled with functions, and runs the function I want. The appropriate function then prints out an answer, that is delivered back to my javascript code which in turn shows the response. The issue is, the function I am attempting to call Must be within the wordpress atmosphere since it calls specific wordpress functions.

Used to do some investigation and located that wordpress offers an ajax handler in admin-ajax.php. I have adopted numerous lessons, including:

http://codex.wordpress.org/AJAX_in_Plugins/

http://www.1stwebdesigner.com/css/implement-ajax-wordpress-themes/

http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/

I have adopted each one of these, but nonetheless for reasons uknown I am obtaining a "-1" response in the admin-ajax.php page. I tracked it and located it arises from the is_user_drenched_in() function. Apparently wordpress does not think my user is drenched in, therefore it errors out for the reason that code block. Here's a number of my code:

This really is my javascript call:

$('button#RunReportButton2').click(function() {
  $('#transactionContainer2').html("<img src='<?php echo $RootDomain; ?>/wp-content/themes/test-client/images/ajax-loader.gif' id='ajaxloader' style='margin: 170px auto auto 340px;' />");
  var fromDate2 = $('#fromDate2').val();
  var toDate2 = $('#toDate2').val();
  $.ajax({ type: "POST",
    url:ajaxurl,
    type:'POST',
    data: { action:"runReport2",
            startingInt:"0",
            fromDate:fromDate2,
            toDate:toDate2 },
    success: function(html) {
      $('#transactionContainer2').html(html);
    }
  });
  return false;
});

I added this to the foot of admin-ajax.php:

add_action(wp_ajax_nopriv_runReport2, runReport2);
add_action(wp_ajax_runReport2, runReport2);

Then my actual php function that's known as is:

function runReport2() {
  include("$RootDomain/wp-content/themes/test-client/reports/historicalTransFunctions.php");

  $startingIndex = $_POST['startingInt'];
  //$startingIndex = 0;
  $fromDate = $_POST['fromDate'];
  //$fromDate = "2011-02-11";
  $toDate = $_POST['toDate'];
  //$toDate = "2011-12-05";

  // post variable sanitization
  if(!is_numeric($startingIndex)) {
    printHistoricalTransactions($token, $client, 0);
    die();
  }

  if($startingIndex <= 0) {
    printHistoricalTransactions($token, $client, 0);
    die();
  }

  // match date
  $dateregex = '/^(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$/';

  if($toDate != "" && $fromDate != "") {
    if(preg_match($dateregex, $fromDate) && preg_match($dateregex, $toDate))
      printHistoricalTransactions($token, $client, $startingIndex, $fromDate, $toDate);
  } else {
    printHistoricalTransactions($token, $client, $startingIndex);
  }
  die();
}

I am wondering if admin-ajax.php is the greatest method what I have to do, and I am also wondering why this is not working? Thanks!

First factor you shouldn't modify core files. There's you don't need to add your ajax function to admin-ajax.php. Just place the add_action right over the function. Also your add_action is missing quotes round the action and performance names (most likely an oversight when posting your code here).

Even the javascript ajaxurl variable isn't on the front-end unless of course a person is drenched in. You have to define the variable inside your js: ajaxurl = 'http://your_site.com/wp-admin/admin-ajax.php';

Frequently with admin-ajax.php I must use output loading to have it to operate right:

 function runReport2() {
 ob_start ();
 //your code

 //end your code
$response = ob_get_contents ();
    ob_end_clean ();
    echo $response;
    die( 1 );