Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9298469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:20:01+00:00 2026-06-18T22:20:01+00:00

I have a created a PHP class that retrieves all the data from a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T22:20:03+00:00Added an answer on June 18, 2026 at 10:20 pm

    This should do it in your javascript:

    $.get('/getCategories',null,function(response){
      console.log(response);
    },"JSON");
    

    And you need to be echo-ing your json encoded data in the php script

    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);
        header('Content-type: text/json');
        header('Content-type: application/json');
        echo $json_data;
    }
    

    I would actually throw that into it’s own thing for re-use later on too:

    public static function send_json_data($php_array)
    {
            header('Content-type: text/json');
            header('Content-type: application/json');
            echo json_encode($php_array);
            exit(); //if you have hooks or something else that executes after output in your script, take this line out.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have gallery.php that loads images with src path from a database. I also
I have created a php person class and a very basic form. The form
I have one class user that is initiated in /update/user.php when update.php is started.
I have created PHP GD image for captcha, file called image.php each time it
I am noob with php so pardon my ignorance. I have created php form
I have created a php/mysql based chat page.It is like gmail chat.once i click
I have created a PHP script and I am lacking to extract the primary
I have created a php table like so: echo <table border = '0' cellpadding
I have created a PHP code to echo the following div <div id=main_catsel> <select
I have created a PHP form which requires the user to select a postcode

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.