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 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 have been trying to create a ListView which I can sort using drag
I have been trying to create a user control library which simply has styled
I have been trying to figure out how to create a treeview which is
Been trying to create a program which basically has a background (Which is painted
I have been trying the fantabulous CsQuery library, which is basically a .NET port
Basically on .show() I've been trying to have all of the inputs convert to
I have been trying for almost a week now to create an SQLite database
I have been trying to serialize a list that contains arrays and lists. I
Recently, I have been trying to accomplish creating a full-width jQuery slider that would
Have been trying to encrypt an xml file to a string so that I

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.