1

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.

2 Answers 2

4

Use json_encode()

addElement('textarea', 'contact_address', 'Address:',
   <?=json_encode(isset($_SESSION['contact_address']) ? $_SESSION['contact_address'] : "" ?>);
Sign up to request clarification or add additional context in comments.

1 Comment

I added a regular expression to remove the escaping of characters such as '. Is there a better way to do this?
1

It sounds like what you want is:

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

Comments

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.