0

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>
4
  • 1
    Please post the relevant code. Commented Apr 17, 2013 at 15:53
  • 2
    I convert all the html with htmlentities() before I push it into the db - this is the wrong way to go about it. You should htmlentities when you're ouputting HTML. Having HTML in your database means your data is poluted with HTML specific encoding. Since you're outputting to JavaScript you should use json_encode (not htmlentities) Commented Apr 17, 2013 at 15:54
  • @jeroen The SyntaxError is in JavaScript, look at the file indicator, it's http. Commented Apr 17, 2013 at 15:57
  • right I have removed the htmlentities() and just used strip slashes when pushing into db. then when i extract from db I addslashes and json_encode() but still nothing Commented Apr 17, 2013 at 17:35

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you I just noticed that the linebreaks where added to the db, i used a str_replace to convert the '\n' to'<br>' and it now works. one other question why does the json_encode add quotes to the stored text?
@BarryWatts - json_encode will essentially turn anything into its corresponding javascript literal. That means strings get quoted and escaped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.