1

I search through the numerous questions already asked about the "unterminated string literal" syntax error but found nothing helping me ...

So, I have this Javascript function :

function parametresModal($parametres) {
        document.getElementById('remodalTest').innerHTML = $parametres;
};

Then I call this function on a link in my page :

<a href="#" onClick='parametresModal("<?php the_field('description-defi'); ?>");'>TEST</a>

The parameter written here is simplified ; I actually want to add this Wordpress ACF's function among others and HTML markup, but I found the issue was appearing with this particular field (see below).

This "parametresModal" function is supposed to fill the following div with its parameters :

<div id="remodalTest">MyDiv</div>

Problem is the console outputs

"SyntaxError: unterminated string literal"

The Wordpress ACF's field "description-defi" contains a few lines of text with some simple quotes (ex. c'est, l'éviter, ...). So I tried to escape the quotes with several methods :

$myField = the_field('description-defi');
$myEscape = json_encode($myField);

or

$myField = the_field('description-defi');
$myEscape = addshlashes($myField);

or

$myField = the_field('description-defi');
$myEscape = htmlspecialchars($myField);

Always resulting in the same error.

Do you see where I could be wrong in my code or my way of thinking the thing ?

Thank you very much !

2
  • 1
    @FetraR. According to the docs the_field() is equal to echo get_field() Commented May 12, 2017 at 8:41
  • @FetraR. that would be effectively what he is doing right now Commented May 12, 2017 at 8:44

1 Answer 1

3

the_field() will output the content of the selected field. If you want to work with a field, you should use get_field() instead.

See: https://www.advancedcustomfields.com/resources/the_field/

Also the newline character will not be escaped by any of PHP's escape functions, if your String contains newlines, you will need to escape them manually using something like this: $myField = str_replace(array("\r\n", "\n"), "<br>", $myField);.

If you know that your DB will consistently use the same newline sequence, you can replace array("\r\n", "\n") by that newline sequence instead.

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

6 Comments

Hi, thank you for your answer. I already tried with $myField = get_field('description-defi'); and escaping $myField, but I got the same error.
(When I told about escaping $myField, I meant with the same "methods" as described in the question : htmlspecialchars, json_encode and addslashes)
Try to also $myField = str_replace(array("\r\n", "\n"), "<br>", $myField);, as accodring to the docs none of PHP's escape function escape newline characters.
Great, it works ! Thank you very much ! (Do you want to post this as an answer so I can accept it ? ;) )
Edited my answer :)
|

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.