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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:11:43+00:00 2026-05-29T21:11:43+00:00

I have been trying to basically create a div for each row of the

  • 0

I have been trying to basically create a div for each row of the json list returned via ajax which would append to the main div #chatcontents. However, i am facing problems with the correct syntax. I think the error is how my json is being returned.

This is the function which does the ajax call in the index.php

function returnValue()
{
 $.ajax({
            async: true,
            type: "GET",
            url: "thread.php",
            data: {lastposted : dateposted},

            success: function (json) {
                if(json)
                {
                for (var i = 0, len = json.length; i < len; ++i) {
                var results = json[i];
                var newDiv = $("<div>" + results.1 + results.2  "</div>");
                }

                   $('#chatContents').append(newDiv);             
                }
            }  
        });
}

thread.php returns a Json result like this..

[{"0":"something","1":"41234","2":"test"},
{"0":"somethingmore","1":"2131","2":"test2"}]

The codes i have seen online seem to return the key but mine are in numberics (0,1,2). I think its coz of the way i have the returned the array. It is a 2d array which was converted to JSON. I am not sure if this is causing the problem as to how i loop through the array in the first page. Any help would be appreciated.

For a clearer pic:
thread.php has this code:

<META HTTP-EQUIV="Refresh" CONTENT="3">
<?php
$latestmsgs = retrieveNewMsgs($lastpost,$currtime);
  ?>

 <?php echo json_encode($latestmsgs);?>

So basically, thread.php is refreshing every 3 seconds. Index.php makes an ajax call to this page and retrieves the latest msg in the form of json and appends.

I hope its clearer.

  • 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-05-29T21:11:45+00:00Added an answer on May 29, 2026 at 9:11 pm

    I think results.1 is invalid syntax, you should use results[1].

    EDIT

    1. You have syntax errors on this line (apart from the bracket syntax instead of dots, you are missing a plus sign):

      var newDiv = $("<div>" + results.1 + results.2  "</div>");
      

      It should be

      var newDiv = $("<div>" + results[1] + results[2] + "</div>");
      
    2. Make sure your callback is getting a parsed JSON object, not a raw JSON string. Alert typeof json inside the callback, it should say “object”. If it says “string”, add dataType: 'json' to the options object passed to $.ajax, and/or make sure the server is setting the proper content-type for the response.

    3. Once you fix that, your code should work. See this jsfiddle.

    EDIT 2

    1. The line with .append should be inside of your for loop, otherwise only the last result on the JSON will be appended to the div. See updated fiddle.

    2. You said on the comments: “I am appending to a class=”container” div”. But your code says you’re appending to a div with ID == “chatContents”. Do you have such div?

    3. When you say an alert stopped working, it means there is probably some error before the alert preventing the rest of the script to execute. Check Firebug’s console for errors, and double-check all code before the alert.

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

Sidebar

Related Questions

I'm trying to create a Textmate snippet, but have run into some difficulties. Basically,
Recently, I have been trying to accomplish creating a full-width jQuery slider that would
I have been trying to implement Win32's MessageBox using GTK. The app uses SDL/OpenGL,
I have been trying to find a really fast way to parse yyyy-mm-dd [hh:mm:ss]
I have been trying to determine a best case solution for registering a COM
I have been trying to work my way through Project Euler, and have noticed
I have been trying to get around this error for a day now and
I have been trying to explain the difference between switch statements and pattern matching(F#)
I have been trying to read a picture saved in Access DB as a
I have been trying to learn multi-threaded programming in C# and I am confused

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.