I have a created a PHP class that retrieves all the data from a database and gets back the results in JSON format:
public function getCategories() {
if (!$this->isConnectionAlive()) {
$this->getConnection();
}
$data = $this->dbconn->prepare("SELECT DISTINCT cat FROM regalo");
$data->execute();
$results=$data->fetchAll(PDO::FETCH_ASSOC);
$json_data = json_encode($results);
}
then an object of the class calls this method and is able to successfully do exactly that:
$dbh = new DatabaseHandler('localhost', 'fakeuser', 'fakepass', 'fakedb');
$dbh->getCategories();
How do I pass this data to my AJAX script so that it can manipulate the JSON-formatted results?
This should do it in your javascript:
And you need to be echo-ing your json encoded data in the php script
I would actually throw that into it’s own thing for re-use later on too: