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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:45:03+00:00 2026-05-21T07:45:03+00:00

I am learning how to use AJAX and PHP json_encode() . I am returning

  • 0

I am learning how to use AJAX and PHP json_encode(). I am returning an array using json_encode() as an AJAX POST response:

$.post("<?php echo base_url();?>index.php/post/post_insert_ajax/",{
        text: post_wall, type:type, entity_id:entity_id, poster_id:poster_id
    },
    function (data){
        var postObj = $.evalJSON(data);                                 
        $("ul#post_already").prepend(//something);
        $("ul#post_already li:first").slideDown("slow");
        $('#post_wall').val("");   
    });

i am using jquery.json.js to explode the array returned, and i wanted to display this block of html into $("ul#post_already")

<a id='"+postObj.post_id+"' href='board/?p="+postObj.post_id+"' >
    "+postObj.post_text+"
</a>
Created on: "+postObj.created_timestamp+"<a class='showCommentBox' id='"+postObj.post_id"' href='#' >Comment</a>                                               
<div id='commentContainer-"+postObj.post_id+"' class='commentContainer' style='display:none;'>
                <form class='submit_comment' id='"+postObj.post_id+"' action='#'>
                    <textarea name='text' id='commentText-"+postObj.post_id+"' class='commentText'></textarea>
                    <input type='submit' name='submit' value='Post Comment' id='"postObj.post_id"' class='submit_comment' />
                    <input type='hidden' name='user_id' id='user_id' value='"+postObj.user_id+"' />
                </form>
            </div>
<ul id='comment_post"+postObj.post_id+"'></ul></li>

I have tried to put all of these into .prepend(), but it seems not to be working that way, what should I do to achieve this? Thank you very much.

UPDATE: it turn out that i missed the concatenate symbol on few of the code, so in the mean time my solution is this:

block = "<b>"+user_name+"</b> posted on ";
block += "<br/><a id='"+postObj.post_id+"' href='<?php echo base_url();?>board/?p="+postObj.post_id+"' >"+postObj.post_text+"</a>";
block += "<br />Created on: "+postObj.created_timestamp+"&nbsp;<a class='showCommentBox' id='"+postObj.post_id+"' href='#' >Comment</a>";
block += "<div id='commentContainer-"+postObj.post_id+"' class='commentContainer' style='display:none;'>";
block += "<form class='submit_comment' id='"+postObj.post_id+"' action='#'><textarea name='text' id='commentText-"+postObj.post_id+"' class='commentText'></textarea>";
block += "<input type='submit' name='submit' value='Post Comment' id='"+postObj.post_id+"' class='submit_comment' />";
block += "<input type='hidden' name='user_id' id='user_id' value='"+postObj.user_id+"' />";
block += "</form></div><ul id='comment_post"+postObj.post_id+"'></ul></li>";

and on success i called the HTML:

$("ul#post_already").prepend(HTML);

it works for now, but i still see this as messy lines, not a best practice, is there any workaround to do this ???

  • 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-21T07:45:03+00:00Added an answer on May 21, 2026 at 7:45 am

    Personally, I like to use the $.parseJSON() method to convert a JSON array into data JavaScript can use. So, back to the answer, say we have an array like this from the server, before it is JSON encoded:

    $array = array('one' => '1', 'two' => '2', 'three' => '3', 'four' => '4');
    

    You can use jQuery to fetch the data then iterate through the data like this:

    var HTML;
    var returnedData = $.parseJSON(data);
    
    $.each(returnedData, function(key, value) {
      HTML += '<li>' + value + '</li>';
    });
    
    $('.unorderedList').append(HTML);
    

    So, that is a basic way to get data from the server, and attach it to a list. Of course, you can change the generated output, or even complicate the JSON array for a more fancy output.

    Hope that helps,
    spryno724

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

Sidebar

Related Questions

I am interested in using/learning RoR in a project where I have to use
I am learning AJAX in zend framework step by step. I use this question
I am learning to use LabVIEW as part of my honours project, and was
I have been learning to use Emacs for a little while now. So far
I am learning to use polymorphism in C#, but cannot figure out this one.
I am learning to use the Dvorak keyboad layout, but I am not good
I'm learning how to use Greasemonkey, and was wondering what the @namespace metadata id
I am learning how to use NUnit. I have my main project in it's
Where is a good place to get started learning how to use jQuery? It
So in my simple learning website, I use the built in ASP.NET authentication system.

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.