Ori
Ori

Reputation: 5081

passing \n into javascript

I have a javascript function that takes in a parameter that is given by a php session.

This works well except when the value is from a text area that contains a newline character. I guess it appears as if I'm passing in a parameter and then the newline indicates I have stopped or something like that. Please advise on how to fix this. I tried using nl2br but it gives a similar error as passing in <br /> is making it think that I forgot to close off the parameter delimiter

addElement('textarea', 'contact_address', 'Address:',   '<?=isset($_SESSION['contact_address']) ? $_SESSION['contact_address'] : "" ?>');

I am guessing I need to get rid of the newline character but I'm not sure of the best way to go about this so that the textarea value maintains its original format.

Upvotes: 1

Views: 377

Answers (2)

VoteyDisciple
VoteyDisciple

Reputation: 37803

It sounds like what you want is:

addElement('textarea', 'contact_address', 'Address:',   '<?=isset($_SESSION['contact_address']) ? trim($_SESSION['contact_address']) : "" ?>');

Upvotes: 1

Greg
Greg

Reputation: 321588

Use json_encode()

addElement('textarea', 'contact_address', 'Address:',
   <?=json_encode(isset($_SESSION['contact_address']) ? $_SESSION['contact_address'] : "" ?>);

Upvotes: 4

Related Questions