1

I am working with laravel, I have string like this in database,

hello
username, address
country

Username and country is in new line, and fields type is 'text' in database,

When i am trying to get that into JavaScript variable like this,

var add="{{ $data['order']->address }}";

Getting error unterminated string literal. No error if string in 1 line in database.

9
  • You can replace the newlines to empty string and then put to the var add. ri8: Commented Mar 4, 2016 at 9:37
  • But for replace string, i have to store that into variable, and when i am trying to store it into variable then getting error. Commented Mar 4, 2016 at 9:38
  • can you call a function in javascript before assigning to a variable like var add="{{ removeNewline($data['order']->address) }}"; Commented Mar 4, 2016 at 9:41
  • no, getting error @Plum Commented Mar 4, 2016 at 9:43
  • whats this $data['order']->address variable, is it php Commented Mar 4, 2016 at 9:47

1 Answer 1

2

First option

You can use ES6 and the new string notation:

var str = `my
           multiline
           string`;

Second option

Replace newlines with \n as described here:

var add="<?=str_replace("\n", '\n', $data['order']->address)?>"
Sign up to request clarification or add additional context in comments.

3 Comments

getting laravel error syntax error, unexpected '{' ... var add="<?php echo $data['order']->address|replace({'\n':'\n'}); ?>";
failed if i hit 'return' key from mac. is it generating any different text?
yes after using this code getting above error when using '\r'

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.