homepage Welcome to WebmasterWorld Guest from 54.197.111.87
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

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




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

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?

 

immols




msg:4094721
 3:24 pm on Mar 10, 2010 (gmt 0)

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.
vol7ron




msg:4095621
 7:27 pm on Mar 11, 2010 (gmt 0)

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.

immols




msg:4095792
 10:05 pm on Mar 11, 2010 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved