1

So I have...

 var newfavz = 'Array (


[0] => [" 6 "," 1 "," 2 "," 5 "," 3 "," 4 "]
[1] => [" 6 "," 1 "," 2 "," 5 "," 3 "," 4 "]
[2] => [" 6 "," 1 "," 2 "," 5 "," 3 "," 4 "]

)
';

on my console.log, but somehow there is a syntax error: unterminated string literal. I've looked around and tried methods such as str_replace "/" with "//" or a regex like .replace(/^/+/g, ''); because it seems like JavaScript don't allow strings to be broken into multiple lines or something like that.

This all started with an SQL query like so...

$favurl = [];
$favquery = "SELECT * FROM userfavs WHERE users = '$username'";
$favresult = mysqli_query($conn, $favquery);

while($row = mysqli_fetch_assoc($favresult)) {

array_push($favurl, $row['fav_id']);

Afterwards, I did

var newfavz = <?php print_r ($favurl); ?>

which led to the above.

Is there any way I could use to solve the syntax error? Thanks!

2
  • 2
    echo result of json_encode instead of using print_r. The printed string is useless in JavaScript environment, Check stackoverflow.com/questions/4885737/… Commented Aug 5, 2017 at 20:55
  • ah sorry I was using print_r when I was doing the debugging :) Commented Aug 6, 2017 at 2:22

3 Answers 3

1

Cause multiline strings are not allowed in js:

"A
 B"

Is a syntax error. You might remove all newlines and replace them with \n, or you use template literals:

`A
 B`

In your code:

var newfavz =` <?php print_r ($favurl); ?>`;

So much about the error. However the string is still unusable, it needs to be parsed. Have a look at JSON or write your own little parser.

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

2 Comments

To add to this, on the server you should encode the array to a json object using json_encode() method before passing to JS.
Hey, I forgot to add this to the question, but I did use a JSON_encode method before I inserted the data into the SQL database. So far, the only way I've found to remove this unterminated string literal error is to do another JSON_encode which is pretty silly IMO.
0

You must put a backslash at the end of each line to have a multiline string:

var newfavz = 'Array (\
[0] => [" 6 "," 1 "," 2 "," 5 "," 3 "," 4 "]\
[1] => [" 6 "," 1 "," 2 "," 5 "," 3 "," 4 "]\
[2] => [" 6 "," 1 "," 2 "," 5 "," 3 "," 4 "]\
)\
';

Comments

0

Either use ES6 template literals:

var newfavz = `Array (


[0] => [" 6 "," 1 "," 2 "," 5 "," 3 "," 4 "]
[1] => [" 6 "," 1 "," 2 "," 5 "," 3 "," 4 "]
[2] => [" 6 "," 1 "," 2 "," 5 "," 3 "," 4 "]

)
`;

Or with a backslash:

var newfavz = 'Array (\n\
\n\
\n\
[0] => [" 6 "," 1 "," 2 "," 5 "," 3 "," 4 "]\n\
[1] => [" 6 "," 1 "," 2 "," 5 "," 3 "," 4 "]\n\
[2] => [" 6 "," 1 "," 2 "," 5 "," 3 "," 4 "]\n\
\n\
)\
';

Or concatenate:

var newfavz = 'Array (\n' +
  '\n' +
  '\n' +
  '[0] => [" 6 "," 1 "," 2 "," 5 "," 3 "," 4 "]\n' +
  '[1] => [" 6 "," 1 "," 2 "," 5 "," 3 "," 4 "]\n' +
  '[2] => [" 6 "," 1 "," 2 "," 5 "," 3 "," 4 "]\n' +
  '\n' +
  ')\n';

\n is used to represent a newline.

1 Comment

Thanks man! I used the `` characters and it worked perfectly!

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.