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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:11:30+00:00 2026-06-07T16:11:30+00:00

Having trouble using ajax to retrieve info from a database. I was getting the

  • 0

Having trouble using ajax to retrieve info from a database. I was getting the code from here:

  1. http://openenergymonitor.org/emon/node/107
    the code from here worked but would only output 1 item
  2. Simple Ajax Jquery script- How can I get information for each of the rows in the table?
    I tried to add this to my code but I could not get it to work.

I am getting everything from a table with the following php:

$result = mysql_query("SELECT * FROM voterecords");  

$data = array();
while ( $row = mysql_fetch_row($result) )
{
  $data[] = $row;
}
echo json_encode($data);

which outputs the following if I navigate to that php page:

[["68","1234","0","1234",""],["69","added with ajax","0","this item was added using ajax",""]]

The format of the above is as follows:
id, title, votes, description, owner

I think that bit all works but I cant be sure because i dont know what JSON is supposed to look like.


Ok now here is the jquery which is supposed to retrieve the info from the JSON and put it into the html element #output

$(function () 
  {

    $.ajax({                                      
      url: 'retrieve.php', data: "", dataType: 'json',  success: function(rows)
      {
        for (var i in rows)
        {
         var row = rows[i];          

          var id = row[0];              
          var name = row[1];
          var votes = row[2];  
          var info = row[3]; 

          $('#output').append("<b>id: </b>"+id+"<b> name: </b>"+name+"<b> votes: </b>"+votes+"<b> info: </b>"+info)
                      .append("<hr />");
    } 
  } 
});

I was expecting this to output all the info but nothing happens.

  • 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-07T16:11:32+00:00Added an answer on June 7, 2026 at 4:11 pm

    Your code is fine except you have a missing closing ) from the callback function.

    Also, in JavaScript, it’s better to place opening braces on the same line, not on the next, as is common in some other languages.

    Corrected/cleaned-up code:

    $(function () {
        $.ajax({url: 'retrieve.php', dataType: 'json'}).done(function(rows) {
            for (var i in rows) {
                var row = rows[i];
                var id = row[0];
                var name = row[1];
                var votes = row[2];
                var info = row[3];
                $('#output')
                    .append("<b>id: </b>"+id+"<b> name: </b>"+name+"<b> votes: </b>"+votes+"<b> info: </b>"+info)
                    .append("<hr />");
            }
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble getting two variables to post using ajax and jquery upon pressing
I am having trouble getting my code to run in a callback using the
I am having trouble getting a success: function(){} working - the ajax code I
I'm having trouble returning data from my controller using an ajax post function. What
Interesting issue here, optimising some code but am having trouble with scope issues using
Ok, so I'm using Jquery's AJAX function and it's having trouble passing a URL
I’m having trouble getting a AJAX/JSON function to work correctly. I had this function
I’m having trouble getting a AJAX/JSON function to work correctly. I had this function
Im having trouble trying to post data correctly using jQuery/AJAX I have the following...
I am having trouble using nested ajax calls with selectors. I have a section

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.