views:

101

answers:

2

hi, 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.

+4  A: 

Use json_encode()

addElement('textarea', 'contact_address', 'Address:',
   <?=json_encode(isset($_SESSION['contact_address']) ? $_SESSION['contact_address'] : "" ?>);
Greg
I added a regular expression to remove the escaping of characters such as '. Is there a better way to do this?
Ori Cohen
+1  A: 

It sounds like what you want is:

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