First pass the array from php using

$row['text'] = <img src="http://ajaxs.ru/demo/js/smail/smail/sleep.png">;//из базы данных $msg[] = array("msg"=>htmlspecialchars($row['text'])); echo json_encode($msg); 

Then I accept an array. Here is the Javascript code:

 var obj = JSON.parse(msg_j); for(var i=0; i < obj.length; i ++){ $('#chatIn').append(obj[i].msg); } 

Displays: <img src="http://ajaxs.ru/demo/js/smail/smail/sleep.png">

And you need to display a picture, not HTML code.

  • and what is it, I apologize, do you have it here? cycle then where? - zb '
  • var obj = JSON.parse (msg_j); for (var i = 0; i <obj.length; i ++) {$ ('# chatIn'). append (obj [i] .msg); } - Origin Game

2 answers 2

The correct js handler is:

 var obj = JSON.parse(msg_j); for(var i=0; i < obj.length; i ++){ var msg = htmlspecialchars_decode(obj[i].msg) $('#chatIn').html(msg); } 

The function "htmlspecialchars_decode" see here

  • Therefore, it is bad to store in the database HTML code ... - uzumaxy
  • If you type so var msg = htmlspecialchars_decode (obj [i] .msg); it’s still blank lines. - Origin Game
  • If so var msg = htmlspecialchars_decode (obj [i] .msg ()); Nothing at all. - Origin Game
  • The bottom line is that you screen HTML with the "htmlspecialchars" function before converting it to JSON. In this form, accept the JavaScript text handler and output shielded HTML. As an option, it is possible to transmit unshielded HTML, but I have never done this and do not know if it will work out. In general, this is a bad decision, forget it. - uzumaxy
  • "obj [i] .msg ()" do not write, this is my typo. Nuna "obj [i] .msg" - uzumaxy

Use the jQuery html method to add. For example:

 $('#chatIn').html(obj[i].msg); 
  • Already tried anyway the same thing - the Origin Game
  • @ Linar Mukhtarov jsfiddle.net/Lh5hm works here - Demyan112rv
  • one
    yes it works like this :) jsfiddle.net/oceog/Lh5hm/1 - zb '26
  • So it works, I substituted it, but after parsing some sort of garbage happens, everything should work, but no ( - Origin Game
  • It is necessary to process the HTML obtained from the database with the htmlspecialchars_decode function - uzumaxy