0

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 ",?

Scimonster
33.5k10 gold badges79 silver badges92 bronze badges
asked Mar 4, 2015 at 5:15

2 Answers 2

2

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")); ?>
answered Mar 4, 2015 at 5:19
Sign up to request clarification or add additional context in comments.

Comments

0

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.

answered Mar 4, 2015 at 5:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.