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

  • Home
  • SEARCH
  • 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 8855321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:06:32+00:00 2026-06-14T14:06:32+00:00

Creating a JS app using jquery. Having real trouble using JSON to get the

  • 0

Creating a JS app using jquery. Having real trouble using JSON to get the data into my script.

have the following data set being created in a .php script;

<?php
$golfer_id=45;
$score_arr[$golfer_id]["name"]="Dave Curry";
$score_arr[$golfer_id][1]=4;
$score_arr[$golfer_id][2]=3;
$score_arr[$golfer_id][3]=5;
$score_arr[$golfer_id][4]=5;
$score_arr[$golfer_id][5]=5;
$score_arr[$golfer_id][6]=5;
$score_arr[$golfer_id][7]=5;
$score_arr[$golfer_id][8]=5;
$score_arr[$golfer_id][9]=5;

$golfer_id=25;
$score_arr[$golfer_id]["name"]="John Doe";
$score_arr[$golfer_id][1]=2;
$score_arr[$golfer_id][2]=2;
$score_arr[$golfer_id][3]=1;
$score_arr[$golfer_id][4]=5;
$score_arr[$golfer_id][5]=5;
$score_arr[$golfer_id][6]=5;
$score_arr[$golfer_id][7]=5;
$score_arr[$golfer_id][8]=5;
$score_arr[$golfer_id][9]=5;

echo json_encode($score_arr);
?>

Eventually this data will come from a mysql query.

This produces:

{"45":{"name":"Bob Test","1":4,"2":3,"3":5,"4":5,"5":5,"6":5,"7":5,"8":5,"9":5},"25":{"name":"John Doe","1":2,"2":2,"3":1,"4":5,"5":5,"6":5,"7":5,"8":5,"9":5}} 

I am using this code

<?php
$hole=1;
$gid=$_REQUEST['gid'];

?>
<script type="text/javascript">

gid= <?php echo "$gid"; ?>+0;
hole = <?php echo "$hole"; ?>+0;

score_arr = new Object() ;
var total = new Array() ;

$(document).ready(function(){

// SET the intial values
$("#hole_num").text(hole);

// Get Data from db
$.getJSON("golf_be.php","", function(score_arr){
// score_arr = jQuery.parseJSON(score_arr);  Tried this it didn't work
console.log(score_arr); //uncomment this for debug
}); //End json
$("#score").text(score_arr[gid][hole]); \\ This has an error 
$("#golfer_name").text(score_arr[gid]["name"]);

Using Firebug i Get this error on the console. interestingly it the object is there.

TypeError: score_arr[gid] is undefined
[Break On This Error]   

$("#score").text(score_arr[gid][hole]);

golf.php?gid=25 (line 94)

Object { 0={...}, 45={...}, 25={...}}

Tried a lot of things but it sure looks to me that for some reason the object from the JSON is not getting into score_arr properly. As you can see I also tried parseJson with no luck.

Some other history, I started with the php array creation in the same script. and using php echo to get the encoded JSON array into the score var directly and everything worked great. Now adapting it to interact with the db.

Any help very appreciated. Bit of a newbie to jQuery and JS and have spent a lot of time searching this great site for an answer with no luck.

  • 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-14T14:06:33+00:00Added an answer on June 14, 2026 at 2:06 pm

    You need to set your text inside the function you are passing into the getJSON call as follows:

    $.getJSON("golf_be.php","", function(data) {
        $("#score").text(data[gid][hole]);
        $("#golfer_name").text(data[gid]["name"]);
    );
    

    This is because the function you are passing into getJSON is a callback function, so it will get executed after the data has been retrieved from your PHP page. As it is in your code it is not setting score_arr in the main function, you are creating a locally scoped variable named score_arr with your data in it, which is only available inside the function you are passing into the getJSON call.

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

Sidebar

Related Questions

I am creating a mobile app using jquery mobile, PHP using JSON for fetching
I am creating an app using jQuery Mobile. I have a listview from which
I am creating an app using jQuery Mobile and PhoneGap. I delegate a button
I am creating an iPad app using XCode 4 and Storyboards. I have a
I've been creating an iPhone App using Core Data. First of all, does it
I have a mobile app I am creating with JQuery Mobile. When a blackberry
I am creating a mobile singlepage web app using jquery mobile. The webapp includes
Creating app with jQuery Mobile, I want a page to load data from a
I'm creating an iOS / Droid app using AJAX, jQuery, and Phonegap. The backend
I am creating a web app using jQuery Mobile and PhoneGap. There is a

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.