0

I am using some facebook post to wall JS, and the information for the post is echoed via php, but i get this error in the console:

SyntaxError: unterminated string literal    

description: 'Well, personal favorite in the following order.<br />

This is the row for the API call

description: '<?php echo $info['definition'] ?>'

and this is $info['definition'] string in the SQL database:

Well, personal favorite in the following order.

1. A stalkers dream come true

2. The reason most work are never done on time.

See what I mean with the examples.

If i use some other string, and type it in directly, it dosnt prompt the script error, how do i escape the string and make it work in the API call?

tkone
  • 22,092
  • 5
  • 54
  • 78
John DOe
  • 212
  • 1
  • 2
  • 12

2 Answers2

3

Try replacing the following line:

description: '<?php echo $info['definition'] ?>'

with this:

description: "<?php echo preg_replace( '~[\r\n]*~', '', $info['definition'] );?>"
web-nomad
  • 6,003
  • 3
  • 34
  • 49
  • error changed to: description: "Well, personal favorite in the following order.<br /> – John DOe May 28 '13 at 12:38
  • Can you please show what are you getting in your html. That would help in debugging. Can you post it to a fiddle so that we could test with it. – web-nomad May 28 '13 at 12:38
  • Dont really know how to pass the exact situation to a fiddle, considering the SQL, but i can give you the page of the error http://www.funkydictionary.com/words/?w=facebook%3D1# – John DOe May 28 '13 at 12:40
  • Sorry, can you remove the `;` from the end of the `description` node...i didn't know it was part of an object. – web-nomad May 28 '13 at 12:49
  • I removed the ; and the issue persists :( – John DOe May 28 '13 at 12:56
  • Sorry, i fixed it. Please check my answer again. The issue was newlines. – web-nomad May 28 '13 at 12:57
  • Thank you for the help, i accepted the answer to, but there is a new issue lol, now it fails on every other page – John DOe May 28 '13 at 13:24
  • In that case, please see this thread (http://stackoverflow.com/questions/168214/pass-a-php-string-to-a-javascript-variable-and-escape-newlines) – web-nomad May 28 '13 at 13:28
0

You have to remove all white spaces from the string. Javascript considers all new lines as a new statement.

Manish Jangir
  • 505
  • 3
  • 9
  • 1
    FYI white space would include the literal [space], so you don't want to remove whitespace, you want to remove line breaks... – tkone May 28 '13 at 12:19