Andris
Andris

Reputation: 1442

jquery Uncaught SyntaxError: Unexpected token ILLEGAL

With $.post send data to external php

Have following code (modified variable names) text_of_the_ad:"<?php echo htmlspecialchars( substr($arr[0]["SomeText"],0,70), ENT_QUOTES, "UTF-8"); ?>",

text_of_the_ad in external php will get with $_POST['text_of_the_ad']

$arr[0]["SomeText"] is text from mysql column SomeText

In Chrome Console see text_of_the_ad:"CITROEN C4 EXCLUSIVE 2.0 HDI DIESEL 5 DOOR HATCH LOW MILEAGE FSH FULL ",

enter image description here

And see error Uncaught SyntaxError: Unexpected token ILLEGAL

Why the error? May be because word FULL is in the next line and after word FSH there is no ",?

Upvotes: 0

Views: 194

Answers (2)

Shan Plourde
Shan Plourde

Reputation: 8726

You could try using the line continuation character, which is \. So that your source would look like:

text_of_the_ad:"CITROEN C4 EXCLUSIVE 2.0 HDI DIESEL 5 DOOR HATCH LOW MILEAGE FSH\
FULL "

Multi-line strings in JavaScript must be escaped, but doing it this way is not going to always produce your desired behaviour - i.e. sometimes browsers might insert newline characters, sometimes they might not.

Upvotes: 0

Scimonster
Scimonster

Reputation: 33399

JavaScript doesn't support multiline strings just like that. The easiest way to hack around this would probably be to just replace a literal \n with \\n before echoing:

<?php echo str_replace("\n","\\n",htmlspecialchars( substr($arr[0]["SomeText"],0,70), ENT_QUOTES, "UTF-8")); ?>

Upvotes: 2

Related Questions