Buy Now
Sign in

Preserve line breaks in TEXT AREA

Chas_L , January 17 2022, 17:39
C
Chas_L 49
January 17 2022, 17:39 #397777
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 65
January 17 2022, 18:35 #397780
Hi Chas,
There is an old post of mine here that might help with this.
Bob
If you'd like to buy me a coffee or two, thank you very much
C
Chas_L 49
January 17 2022, 19:30 #397781
Hi Bob,
thanks for that. I assume in the code you suggested I try:
<?php$form->data['input_name'] = nl2br($form->data['input_name');?>
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:
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>

<?php
$form->data['message'] = nl2br($form->data['message');
?>
<?php
$form->{data:message} = nl2br($form->{data:message};
?>
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.; ?>


Any suggestions? I'm not a wiz with PHP as you can probably tell.

Chas
C
Chas_L 49
January 20 2022, 14:58 #397809
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 65
January 21 2022, 15:59 #397813
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
If you'd like to buy me a coffee or two, thank you very much
C
Chas_L 49
January 21 2022, 16:27 #397814
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
C
Chas_L 49
January 21 2022, 16:35 #397815
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.
C
Chas_L 49
January 21 2022, 16:51 #397816
OK, so switched form to advanced type, switched to Actions and added a PHP block with the following code inside:
<?php$form->data['message'] = nl2br($form->data['message');?>
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.
C
Chas_L 49
January 21 2022, 17:13 #397817
OK, so tested the PHP module with simple server side PHP code and it works, the <?php and ?> are not needed.
However, just entering
$form->data['message'] = nl2br($form->data['message');
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 65
January 22 2022, 10:23 #397818
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
If you'd like to buy me a coffee or two, thank you very much
C
Chas_L 49
January 22 2022, 11:36 #397819
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
// New data maintain line breaks

$form->data['message'] = nl2br($form->data['message']);

echo "Thanks, that was great!<br>";
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
Line test number 2
Line test number 3
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.
C
Chas_L 49
January 23 2022, 08:32 #397829
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 65
February 01 2022, 15:17 #397894
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
If you'd like to buy me a coffee or two, thank you very much