Barry Watts
Barry Watts

Reputation: 794

jquery ajax to mysql to php to javascript conversion

I have a textarea on a page that has its contents pushed into a mysql db with ajax/jquery $.post. then the mysql data is called with php and then pushed into a new dynamic textarea with javascript.

I convert all the html with htmlentities() before I push it into the db.

Then I run a mysql_query to drag out the data. At this point if I echo the data it echos fine.

When I then push it into a js function to create the new textarea and add the data this is where I get the error. if I try to alert the data that I got with the mysql_query I get nothing.

I am just using some Lorem Ipsum text to test. I have run firebug and the error I get is

[17:44:20.948] SyntaxError: unterminated string literal @ http://**************.com/********.php:427

I wont post all code unless anyone needs it as there is a lot and to be honest i think its some kind of escape/html encode type problem.

there is the last js function that gets the above error as per CTRL 'U'

 <script>NewTextArea('1','draggable','176','672','300','300','&lt;strong&gt;Lorem Ipsum&lt;/strong&gt; es simplemente el texto de relleno de las imprentas y archivos de texto. Lorem Ipsum ha sido el texto de relleno est&Atilde;&iexcl;ndar de las industrias desde el a&Atilde;&plusmn;o 1500, cuando un impresor (N. del
 T. persona que se dedica a la imprenta) desconocido us&Atilde;&sup3; una galer&Atilde;&shy;a de textos y los mezcl&Atilde;&sup3; de tal manera que logr&Atilde;&sup3; hacer un libro de textos especimen. No s&Atilde;&sup3;lo sobrevivi&Atilde;&sup3; 500 a&Atilde;&plusmn;os, sino que tambien ingres&Atilde;&sup3; como texto de relleno en documentos electr&Atilde;&sup3;nicos, quedando esencialmente igual al original. Fue popularizado en los 60s con la creaci&Atilde;&sup3;n de las hojas &quot;Letraset&quot;, las cuales contenian pasajes de Lorem Ipsum, y m&Atilde;&iexcl;s recientemente con software de autoedici&Atilde;&sup3;n, como por ejemplo Aldus PageMaker, el cual incluye versiones de Lorem Ipsum.');</script>

Upvotes: 0

Views: 221

Answers (1)

Sam Dufel
Sam Dufel

Reputation: 17608

When you're injecting php variables into javascript, it's safest to wrap them in a call to json_encode(). Javascript has implicit line endings, which means newlines in strings need to be escaped. If you echo a php string with a newline as part of your javascript block, it'll break the script.

Upvotes: 3

Related Questions