-1

I'm fetching the data from my database through 'fetching' datatype, 'Features' is a row, element in the 'Features' row has Multiline data.

eg:

Simultaneous HD video and image recording, geo-tagging, touch focus.

face and smile detection.

image stabilization, HDR.

php

function mf(){
    while($row = mysqli_fetch_array($fetching)){
        echo $row['Features'];
}
}

Javascript

document.getElementById("idname").innerHTML = "<?php mf(); ?>";

Html

<div id="idname"> </div> 

Here mf function has Features row element shown in example. When I try to execute, no output in webpage, If I remove new line and write whole element in a single line it works, if it is multiline, then data is not showing up in webpage. Please help me!

Sailesh Kotha
  • 1,939
  • 3
  • 20
  • 27

3 Answers3

0

Because the string has newlines, the JavaScript looks like this:

... .innerHTML = "text text
text
text"

Which is not valid JS. In PHP, replace the newlines with <br />, so innerHTML will parse instead of a syntax error.

rvighne
  • 20,755
  • 11
  • 51
  • 73
  • Thank you for the reply. But, I cant update my whole database by removing all new lines and adding
    it takes lot of time. Any alternate solution please
    – Sailesh Kotha Aug 03 '14 at 20:43
0

If you want to use \n new line character, update the innerText property. If you insist on using the innerHTML property, you must replace the \n characters with HTML break tags <br />

http://jsfiddle.net/6QnQG/

Unless you are certain that the data doesn't potentially contain malicious HTML or don't need to output html tags, I'd recommend you go with innerText.

document.getElementById('idname').innerText = "<?php mf(); ?>";

cbayram
  • 2,259
  • 11
  • 9
  • Thank you for the reply. But, I cant update my whole database by removing all new lines and adding /n it takes lot of time. Any alternate solution please – Sailesh Kotha Aug 03 '14 at 20:54
  • @sailesh \n is new line. I'm confused, does your output have \n new line characters or not? If yes, innerText will work for you. If not, then you don't have a multiline content. See update – cbayram Aug 03 '14 at 21:07
  • @cbayran Thanks for the reply, but data in feature has new lines as shown in example. I was unable to view that data in features in browser, instead I can view it in page source – Sailesh Kotha Aug 03 '14 at 21:10
  • @sailesh and doing innerText on this data doesn't work? ```document.getElementById('idname').innerText = "";``` – cbayram Aug 03 '14 at 21:16
  • What does your new line character look like in the page source? If you have the \n new line character in your String, the innerText property will work. – cbayram Aug 03 '14 at 21:20
  • Nope, not working. I can give you my teamviewer id, where I can clearly tell you what problem is. – Sailesh Kotha Aug 03 '14 at 21:53
0

You can replace the new lines with <br/> in javascript.

var str = "<?php mf(); ?>";
str = str.replace(/(?:\r\n|\r|\n)/g, '<br />');
document.getElementById("idname").innerHTML = str;

regular expression taken from: How do I replace all line breaks in a string with <br /> tags?

Community
  • 1
  • 1
Juan Techera
  • 1,192
  • 2
  • 14
  • 25