0

I want to create comments using jquery , this is the php file that ajax calls :

require_once '../Classes/MainController.php';
$mc = MainController::getInstance();
foreach ($mc->getThreadComments($_POST['thread_id']) as $comments)
print json_encode($comments);

This is the output from the ajax call (2 comments):

{
    "0": "6",
    "id": "6",
    "1": "10",
    "user_id": "10",
    "2": "3",
    "thread_id": "3",
    "3": "ba nu fii    nebuuun cum e piesa asta !",
    "comment": "Damn son, this is the shit!"
} {
    "0": "7",
    "id": "7",
    "1": "10",
    "user_id": "10",
    "2": "3",
    "thread_id": "3",
    "3": "Omg this hits so  fucking hard , bass too stronk !",
    "comment": "Omg this hits so fucking hard , bass too stronk !"
}

And I want to convert this into html , somthing like :

 <div class="large-2 columns small-3"><p> User id </p></div>
 <div class="large-10 columns"><p > Comment </p></div>

How would the script look like , if I would like it to loud automatically once the page is loaded ? I know it has something to do with each() , but I don't really know how to use it.

CharliePrynn
  • 3,034
  • 5
  • 40
  • 68
Beriu
  • 59
  • 7
  • I've looked at it and I'm not that advanced in jquery , could you please possibly link a more basic article on how to do this , if you by any means have seen it before ? – Beriu Aug 28 '14 at 09:10

1 Answers1

1
            $.post("<?php echo site_url("assignment/getcomments"); ?>" 
                    ,{ "csrf_aimsis":csrf_key, 
                       "assignment_id":assignment_id,
                       "start": start
                     },
                function(data)
                {
                    if(data != null)
                    {
                        if(data.error == undefined)
                        {
                            for(var i = 0; i < data.length; i++)
                            {
                                var wrapper = $("<div></div>");

                                var div_1 = $("<div></div>");
                                div_1.attr("class", "large-2 columns small-3");
                                div_1.append(data[i].user_id);

                                var div_2 = $("<div></div>");
                                div_2.attr("class", "large-10 columns");
                                div_2.append(data[i].comment);

                                wrapper.append(div_1);
                                wrapper.append(div_2);

                                $(document).append(wrapper);


                            }
                        }
                        else
                          alert(data.error);
                    }

                  $(".loading").hide();
                },"json").error(errorAjax); 

first make an ajax call, then loop throught it's data and append to a wrapper div, and last append to document itself or u can append it to certain element.

NomNomNom
  • 811
  • 3
  • 12
  • 37
  • the `function(data)` goes in the `success:` part of ajax ? – Beriu Aug 28 '14 at 10:04
  • it's the return data of ajax. – NomNomNom Aug 28 '14 at 10:11
  • `$.post` picks up automatically the return data ? Just trying to understand – Beriu Aug 28 '14 at 10:17
  • in the controller file which called by ajax, u did json_encode() right? it will be ur data which will be returned. have u try it? if u just trying to understand without trying, u won't understand. – NomNomNom Aug 28 '14 at 10:33
  • If you have some spare time (5-7 min) could you add me to chat so that I may show you what I've done? – Beriu Aug 28 '14 at 10:37
  • I did without the `$.post` because I couldn't make it work , and there where a lot of conflicts , so I trie to add the `function(data)` to the `success:` part of ajax – Beriu Aug 28 '14 at 10:47