Welcome to WebmasterWorld Guest from 54.163.68.15

Forum Moderators: open

Message Too Old, No Replies

\n added to text retreived (by ajax) from mySQL

   
3:15 pm on Mar 10, 2010 (gmt 0)

5+ Year Member



Hello guys

In my MySQLdatabase I find this (which is good):
<p>Hier een tekst met single quotes zoals 'deze' en double quotes zoals "deze"</p>
<p>En dan nog een alinea om te proberen.</p>


On my webpage i find this, which is unexpected:
<p>Hier een tekst met single quotes zoals \'deze\' en double quotes zoals \"deze\"</p>
\n
<p>En dan nog een alinea om te proberen.</p>


I use this code to retrieve stuff from the database (it's a generic function, i cut out the ifs and cases etc):
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
$data[] = $row;
return $data[0]['value'];
}


And this to add it to the page
document.getElementById(id).innerHTML = "<textarea>"+ajaxRequest.responseText+"<\/textarea>"


Either with retrieving from db or with displaying responsetext, the \ and \n are added. Can you tell me where and how to get rid of them? I assume there is some php function for it, or some manner of retrieving data that prevents it from doing so?
3:24 pm on Mar 10, 2010 (gmt 0)

5+ Year Member



BOh... wait... i show the stuff without using ajax too, in that case it shows what i expect (the stuff without the \n and excaped quotes.

So only when using
ajaxRequest.responseText
stuff is added.
7:27 pm on Mar 11, 2010 (gmt 0)

5+ Year Member



Are you using PHP or Perl?

One reason you're seeing it is because you're outputting to a <textarea>, which may show those characters.

You'll probably have to do some preprocessing on the ajaxRequest.responseText.

Maybe: ajaxRequest.responseText = ajaxRequest.responseText.replace(/\\n/g,"");

Note the "\\" used, you may have to change it to just "\", depending on if you're using PHP or Perl, and if your JavaScript is embedded in it.
10:05 pm on Mar 11, 2010 (gmt 0)

5+ Year Member



Thank you, Vol7ron for your reply.

I am using PHP.
Problem with using a regex to find the 'bad' stuff before outputing it to the textarea is that i do not know what i need to replace. There could be \', \", \n and maybe more.

I guess just replacing \n wit nothing first, and \ after that will be good enough to start with.

I am happy to know it's the textarea that's causing the confusion. Thanks for that.