0

I am returning a PHP array back to a JQuery call for appending into a div called "name-data". I want my array to be listed vertically, so I concatenate a br tag in the PHP however, when it gets to the HTML page the br is not being rendered, it just comes out as text. I have tried the various forms of br all without luck.

I am new to JQuery - What am I doing wrong ? Many Thanks !

PHP:

$result = mysqli_query($con,"SELECT FirstName FROM customer limit 5");
while($row = mysqli_fetch_array($result))
 {
echo $row['FirstName']."<br />";
}

JQuery:

$('input#name-submit').on('click',function(){
var name = $('input#name').val();
if($.trim(name) !=''){
    $.post('search.php',{name:name}, function(data){
    $('div#name-data').text(data);
    });
}
});

HTML

Name:<input type="text" id="name">
<input type="submit" id="name-submit" value="grab">
<div id="name-data"> </div>
Vince
  • 1,405
  • 2
  • 20
  • 33

3 Answers3

5

Try replacing .text() with .html().

$('div#name-data').html(data);

Also, for the love of Cthulhu, parameterize your MySQL query. That's a SQL injection just waiting to happen.

Community
  • 1
  • 1
Christian Ternus
  • 8,406
  • 24
  • 39
  • Many Thanks !! This is not production. I'm learning JQuery however your advice is well taken ! – Vince Oct 28 '13 at 21:54
3

Change:

 $('div#name-data').text(data);

to

$('div#name-data').html(data);

Read about .text() and .html()

Deepak
  • 6,684
  • 18
  • 69
  • 121
0

Also, if the <br> is the last thing in the div, most browsers won't actually render a break. You need something after it, like this: <br>&nbsp;

elixenide
  • 44,308
  • 16
  • 74
  • 100
  • 1
    No, that's not the reason. The OP is using .text() which only sets the text, it does *not* insert HTML. – ComFreek Oct 28 '13 at 21:51
  • Hence the word "also." Even if OP uses `.html()` instead of `.text()`, this is another likely problem in most browsers. – elixenide Oct 28 '13 at 21:53