Forums

Preserve line breaks in TEXT AREA

Chas_L 17 Jan, 2022
Hi, when a message is sent, any line breaks or new lines are lost in the text area of the message when it arrives.

I know this can be resolved by allowing the text area to be a Rich Text field, but I'd rather not do that.

Is there any way to preserve the new line / line breaks that the visitor enters into the text area of the form?

Any suggestions most welcome.
Thanks
Chas.
GreyHead 17 Jan, 2022
Hi Chas,

There is an old post of mine here that might help with this.

Bob
Chas_L 17 Jan, 2022
Hi Bob,

thanks for that. I assume in the code you suggested I try:[pre]<?php$form->data['input_name'] = nl2br($form->data['input_name');?>[/pre]
that it takes the content of 'input_name' and formats it back into the same field. Yes?

I've tried to use the suggestion in two ways but have obviously not got the code right. It seems the PHP is not being run. How do I enable PHP in the message area?

Here's what I've entered into the message box:[pre]we have received the following message from the website:
<br><br>[br]{date:d-m-Y H:i}<br>[br]NAME: {data:name}<br>[br]EMAIL: {data:email}<br>[br]<br>[br][br]MESSAGE: {data:message}<br><br>[br][br]<?php[br]$form->data['message'] = nl2br($form->data['message');[br]?>[br]<?php[br]$form->{data:message} = nl2br($form->{data:message};[br]?>[/pre]
Here's what I received from a test email which included two line breaks and an extra line feed.
we have received the following message from the website: 

17-01-2022 19:27
NAME: Bert
EMAIL: bert@nomail.co

MESSAGE: This is another test message. This is on the next line down. This is two lines further down.

data['message'] = nl2br($form->data['message'); ?> This is another test message. This is on the next line down. This is two lines further down. = nl2br($form->This is another test message. This is on the next line down. This is two lines further down.; ?> [br]
[br]Any suggestions? I'm not a wiz with PHP as you can probably tell.
Chas
Chas_L 20 Jan, 2022
Hi Bob,

Seems I can't run PHP within any page on my site, unless the page is a .php page, so embedding won't work.

Is there a preferred Javascript I could use to convert the text into a format that preserve's line breaks / new lines?

Thanks
Chas
GreyHead 21 Jan, 2022
Hi Chas,

Your form needs to be in Advanced mode (not just the basic Contact form). In the Pages view click the 'Switch to Actions View' button, then in the Submit Page click the Advanced Actions menu and drag a PHP action into the page. It will need to be before the Email action so that the modified data is available.

Bob
Chas_L 21 Jan, 2022
Hi Bob,
I thought there might be something like that.
There does not appear to be a switch on the standard form, unlike when you create a new ADVANCED form.



Is there a simple convert option anywhere or do I have to create a new one from scratch?
Thanks
CHas
Chas_L 21 Jan, 2022
AH, found it. Have to go to Settings and change the form TYPE to ADVANCED first, then save and reload then the SWITCH TO ACTIONS EDITOR button becomes visible.
Chas_L 21 Jan, 2022
OK, so switched form to advanced type, switched to Actions and added a PHP block with the following code inside:
​[pre]<?php$form->data['message'] = nl2br($form->data['message');?>[/pre]
Sadly the form just gives errors when viewing in Front or Admin views. Removing the block the form works fine.

What code do I need to put in the block apart from the above and how do I make sure "It will need to be before the Email action so that the modified data is available"?

Thanks again.
Chas_L 21 Jan, 2022
OK, so tested the PHP module with simple server side PHP code and it works, the <?php and ?> are not needed.

However, just entering [pre]$form->data['message'] = nl2br($form->data['message');[/pre]
causes it to fail. as does replacing the ' with ` or ", each time the admin view shows:

0 syntax error, unexpected ':'

even copying the ' from the error message results in the same error, so obviously I've got the syntax wrong somehow?
GreyHead 22 Jan, 2022
Hi Chas,

My apologies, I've mistyped a ] as ) near the end of the code. Please try correcting that.

If you still get an error then you might try replacing all the square brackets with () - that would be needed if the form data were in an object rather than an array.

Bob
Chas_L 22 Jan, 2022
Hi Bob,

Thanks for that. I tried a number of different combinations of the code and the only one that does not give an error is
​[pre]// New data maintain line breaks

$form->data['message'] = nl2br($form->data['message']);
[br]echo "Thanks, that was great!<br>";[/pre]

I included an echo to show on the Thank You message that the code had been read and executed.

In the email message I use:
we have received the following message from the website:
<br><br>
{date:d-m-Y H:i}<br>
NAME: {data:name}<br>
EMAIL: {data:email}<br>
<br>

MESSAGE: {data:message}<br><br>
And that all seems to work, except that in the test I enter the following into the message box

Line test number 1[br]Line test number 2[br]Line test number 3[br]Line test number 4

but what I receive is:

MESSAGE: Line test number 1 Line test number 2 Line test number 3 Line test number 4

So thinking that what I was getting back was the original message box text, I changed the PHP so that what was sent to the form was the result of the conversion in a new variable called message_new
$form->data['message_new'] = nl2br($form->data['message']);
and the email message to
MESSAGE: {data:message_new}<br><br>
Sadly now there is nothing in the received email message area. Tried again with
$form->data['message'] = nl2br($form->data['message_new']);
just in case I had the result and the original the wrong way round but that was also blank.

As a test, I tried echoing the content of the message onto the thanks display
echo "Message Content: " . $message . "<br>";
And no matter which I used, it does not show anything for that variable, so it's obviously not transferring the original form message into the PHP. Is there something wrong with how the form data field 'message' is being read by the PHP?

Using
$_POST['message'] = nl2br($form->data['message_new']);
grabs the content of the variable "Message" but it does not pass the content to message_new.

I'm stumped now.
Chas_L 23 Jan, 2022
Latest update. Have now correctly got the formatted data from the form into a PHP variable using this:
$newmessage = nl2br($_POST['message']);
echo "Message content: $newmessage <br>";
The echo test displays the text of the message correctly like:

Message content: Test line 1
Test line 2
Test line 3
Test line 4

What I need to do now, is pass the content of $newmessage to the email content.
GreyHead 01 Feb, 2022
Hi Chas,

If you have your PHP return the value then you should be able to pick it up in the Email body using a shortcode like {var:xxx} where you replace xxx with the PHP action name.

Bob
You need to login to be able to post a reply.