I have a number of php files in my WordPress plugin the main file I am having issues with has a class in it and I can call one method from it as my display page loads and that works fine but when I try and do it from a third file from my jQuery post I get the error sated in the title of my question. The error is said to be in the file with the class but I can’t see an error. I have made sure that global $wpdb
has been added to every function so and the first function works to get the track names but doesn’t if I call it again through my last code as a test. Any help would be appreciated.
Class file
<?php
class SelectList
{
public $tableName;
public $driverTableName;
public $classTableName;
public $posTableName;
public $trackTableName;
public function __construct()
{
global $wpdb;
$tableName = $wpdb->prefix . "raceresults";
$driverTableName = $wpdb->prefix . "driverData";
$classTableName = $wpdb->prefix . "classData";
$posTableName = $wpdb->prefix . "posData";
$trackTableName = $wpdb->prefix . "trackData";
}
public function ShowTrack()
{
global $wpdb;
$category = '<option value="0">choose...</option>';
$postids = $wpdb->get_col("SELECT trackName FROM wp_trackData;");
foreach ($postids as $value)
{
$category .= '<option value="' . $value . '">' . $value . '</option>' ;
}
return $category;
}
public function ShowDate($track)
{
global $wpdb;
$type = '<option value="0">choose...</option>';
$postids2 = $wpdb->get_col("SELECT DISTINCT raceDate FROM wp_raceresults WHERE trackName = '" . $track . "';");
foreach ($postids2 as $value2)
{
$type .= "<option>" . $value2 . "</option>";
}
return $type;
}
}
$opt = new SelectList();
jQuery Code
jQuery(document).ready(function()
{
jQuery("select#kDate").attr("disabled","disabled");
jQuery("select#kTrack").change(function()
{
jQuery("select#kDate").attr("disabled","disabled");
jQuery("select#kDate").html("<option>wait...</option>");
var id = jQuery("select#kTrack option:selected").attr('value');
jQuery.post("<?php echo plugins_url("/race-results/getResults.php"); ?>", {id:id} , function(data)
{
jQuery("select#kDate").removeAttr("disabled");
jQuery("select#kDate").html(data);
})
.success(function() { alert("success"); })
.error(function(xhr, status, detail) { alert("error ("+status+") : " + detail); })
});
});
Get Result
<?php
include "popDrp.php";
echo $opt->ShowDate($_POST[id]);
?>
The problem with your code is that you’re not “bootstraping” your wordpress so you don’t have loaded properly the wordpress object and classes when you do your ajax request. A simple (but not correct) solution is to add at the beginning of your
getResults.php
file something like this:But much better, read this articles: