0

Hi,
I want to take php string in javascript variable.
How to take it?
php string includes \n so while taking it in variable it shows me error of "SyntaxError: unterminated string literal"
I got one solution form stackoverflow that use json_encode
but after use of json_encode is giving me string by adding extra \n
for example
my string is

I have done many applications in this skill.

Lastest work is done in GS Lab.
So sending this for manager approval.

but on use of json_encode it is giving me

    I have done many applications in this skill.



    Lastest work is done in GS Lab.

    So sending this for manager approval

code is as below:

var _previous_description='<?php echo json_encode($previous_description); ?>';
var desc = _previous_description.substring(1, _previous_description.length-1); // it is to remove extra "" added while encoding at the start and end of string
alert(desc); // it gives me strinf with extra spacces.
// current values
var c_description=$('#previous_description').val();
alert(c_description); // it gives correct value

What is wrong here?
how to remove this extra lines?
or is there any simple solution for this?

1
Contributor
1
Reply
10
Views
3 Years
Discussion Span
Last Post by Priti_P
0
var _previous_description='<?php echo json_encode($previous_description); ?>';
var desc = _previous_description.substring(1, _previous_description.length-1);
desc=desc.replace(/(\r\n|\n|\r)/gm,"\n");

This worked for me.
Kindly let me know in any case it fails.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.