javascript - how to display JSON data in html div -


here json string:

[[{"name":"pepe pinedo"},{"message":"something"},{"datein":"2015-07-01 11:12:34"}],[{"name":"pepe pinedo"},{"message":"something"},{"datein":"2015-07-01 11:14:30"}]] 

here's js code

$.post(url, function( data ) { //fire ajax post request     alert("got messages: " + data); //                              /*$.each(data, function(index,e){         content += e.name + ':'+ e.message +'<br>';         $("#chatbox").append(content);     });*/     }); 

i trying make messages this:

enter image description here

php

$messages = $this->person_model->get_msg();  foreach ($messages $i => $valor) {                     $fila['name']       = $valor->names;                     $message['message'] = $valor->message;                     $fecha['fecha']     = $valor->datein;                     $data[] = array($fila, $message,$fecha);              }             echo json_encode($data);  

any appreciated

first of all, data structure make more sense this:

[   { "name": "rildo", "message": "something", "date": "2015-07-01 11:12:34" },   { "name": "rildo", "message": "something", "date": "2015-07-01 11:14:30" } ] 

to so, modify php bit:

$messages = $this->person_model->get_msg();  foreach ($messages $i => $valor) {     $data[] = array(         "name"    => $valor->names,         "message" => $valor->message,         "date"    => $valor->datein     ); }  echo json_encode($data);  

then, display it:

$.post(url, function( data ) { // fires ajax post request      alert("got messages: " + data); // alerts string      var messages = json.parse(data), // turns string object can manipulate         content = '';      $.each(messages, function(index,e){         var time = e.date.substr(-8,5); // extracts time date         content += time + ' ' + e.name + ': ' + e.message + '<br>';     });       $("#chatbox").append(content);  }); 

demo

var messages = [          { "name": "rildo", "message": "something", "date": "2015-07-01 11:12:34" },          { "name": "rildo", "message": "something", "date": "2015-07-01 11:14:30" }      ],      content = '';    $.each(messages, function (index, e) {      var time = e.date.substr(-8,5);      content += time + ' ' + e.name + ': ' + e.message + '<br>';  });    $("body").append(content);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -