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 9323239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T04:17:45+00:00 2026-06-19T04:17:45+00:00

I am trying to get data from an SQL Database through jQuery, Ajax and

  • 0

I am trying to get data from an SQL Database through jQuery, Ajax and PHP.

Here is the jQuery Code

$.ajax({
    url: 'OpenQuiz.php',
    data: '',
    dataType:'json',
    success: function(data) {
        var one = data[0];
    }
}

$(document).ajaxComplete(function(event,request,settings) {
    alert("check");
}

Here are the json encode lines at the end of the PHP file called “OpenQuiz.php”

echo json_encode($QuizName);
echo json_encode($options);
echo json_encode($votes);
echo json_encode($row_num);
echo json_encode($percentage);
echo json_encode($TruncPercentage);

Please note:
$options, $votes, $Percentage and $TruncPercentage are all two-dimensional arrays.
$row_num is an integer.
$Quiz_Name is a single-dimensional array.

I find that the jQuery runs fine and the ajax request is called because the alert box for “check” shows up. The problem is that I dont know how to access the variables after they have been transferred. I know that it has something to do with data[0] but I don’t really understand what “data[0]” means or what it represents. Basically how to I access the variables that I have sent using json_encode in the PHP file?

  • 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-19T04:17:46+00:00Added an answer on June 19, 2026 at 4:17 am

    data[0] is the first json_encoded item in the array returned. You shouldn’t json_encode everything separately, you should build an array and json_encode this.

    $items = array('QuizName'=>$QuizName,'options'=>$options, ... ,'TruncPercentage'=>$TruncPercentage);
    
    echo json_encode($items);
    

    Then you retrieve with:

    success: function(data) {
        var qn = data.QuizName,
            opt = data.options;
    }
    

    And so on for each item, with data.[whatever], and [whatever] being the name of the item you gave it in the array. I know I originally said to retrieve by index (data[0]), but I prefer to retrieve explicitly so that the code is more maintainable.

    As a side note, you can eliminate the datatype:'json' declaration in your ajax call by simply setting the header correctly on the PHP side:

    header('Content-type: application/json');
    

    Placing that at the top of the document will force the server to recognize the page as json. Far more consistent and explicit than having jQuery look for it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm trying to get data from my sql database and sort it by
I'm trying to get character data from www.wowarmory.com using PHP and cURL. The code
Simply trying to update my sql database via php and jquery ajax. It is
I am trying to get data back from a PHP file assigned with GET
Using php I am trying to get some data from a .csv file which
for example I'm trying to get the data from database like: using (ExplorerDataContext context
I'm trying to select data from the database and I have the following code
I'm trying to import data from a Progress database into a MS SQL 2005
I'm simply trying to get data from two sql server db tables using ado.net
I am trying to store data in created SQLite database. Here is the code

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.