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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:22:14+00:00 2026-06-01T03:22:14+00:00

UPDATE: Based on suggestions here, I’ve modified my code. To get a handle on

  • 0

UPDATE: Based on suggestions here, I’ve modified my code. To get a handle on how this works, I am only trying to alert the PHP array with JSON. Still cannot get it to work. The code below resides on the same test.php page. If refreshed, should the alert fire? The function seems to fail as well. The TEST alert does not work.

<?php
  header('Content-type: application/json');
  $arr = array ('item1'=>"test1",'item2'=>"test2",'item3'=>"test3");
  echo json_encode($arr);
?>

<script type="text/javascript">
  $(function() {
    $.getJSON('test.php',function(data) {
      alert("TEST");

      $.each(data, function(key, item) {
        alert(key + item); }); 
      });
    });
  });
</script>

I am new to JSON and trying to get a PHP array to display in an HTML file. Below is a simple example. Please tell me if this should work and what I am doing wrong. I am trying to output the keys in this array. Thanks.

My PHP script (test.php):

     $arr = array ('item1'=>"test1",'item2'=>"test2",'item3'=>"test3");
json_encode($arr);

$content = file_get_contents('test.htm');
echo $content;

My HTML page (test.htm):

<script type="text/javascript">

  $(function() {
$.getJSON('test.php',function(data) {
  $.each(data,function(i, item) {
    alert(item.key);
      });
   });
});

 </script>
  • 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-01T03:22:16+00:00Added an answer on June 1, 2026 at 3:22 am

    Make sure you have included jQuery in your HTML file.

    As you are only alerting keys, you don’t have to wait for a dom-ready event – and even if you wanted to, I would first get the JSON and put the $(function... into the ajax callback.

    Your JSON-encoded PHP-array should look like this:

    {"item1":"test1","item2":"test2","item3":"test3"}
    

    Check about this in your debugger, or load test.php directly in your browser. As others have suggested, your PHP code with the file_get_contents is a bit odd.

    Last, your object traversal won’t work. Look at the docs:

    $.each(data, function(key, item) {
        alert(key + item);
    });
    

    The string concatenator in JavaScript is the +, not the . as in PHP. item.key would try to access the key property of one of your items – none has such one as thy are strings.

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

Sidebar

Related Questions

The same as this question but for java Update Based on the comments and
I continually get these errors when I try to update tables based on another
I'm trying to update one table based on values in another table. What's wrong
What I'm trying to do here is to create a list of groups based
Update: Based on the answers I initially went the route of using IsInstanceOf() which
Update: Based on a couple of the answers I have received, I just want
We have an application containing a lot of user controls that update frequently based
I'd like to update a page based upon the results of multiple ajax/json requests.
I see that EF can update a model based on an existing database schema.
I have a case where I need to update a jqgrid based on some

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.