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!

HotPotatos
  • 13
  • 2
  • 3
  • 2
    `echo` result of `json_encode` instead of using `print_r`. The printed string is useless in JavaScript environment, Check https://stackoverflow.com/questions/4885737/pass-a-php-array-to-a-javascript-function – Ram Aug 05 '17 at 20:55
  • ah sorry I was using print_r when I was doing the debugging :) – HotPotatos Aug 06 '17 at 02:22

3 Answers3

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.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • To add to this, on the server you should encode the array to a json object using json_encode() method before passing to JS. – Adrian Aug 05 '17 at 20:57
  • 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. – HotPotatos Aug 06 '17 at 02:18
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.

Sven
  • 5,155
  • 29
  • 53
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 "]\
)\
';
DYZ
  • 55,249
  • 10
  • 64
  • 93