Log in

View Full Version : Resolved using textarea for a message in an email



mcolton
09-17-2014, 03:49 PM
I have a textarea in a form to create a field called $message. When the user hits "submit", I want to use this field as the message in an email I send out.
I'm getting the $message in the php code that is called but the formatting is missing.

The field is entered as (I'm hitting a carriage return after each line) :
line 1
line 2
line 3
etc

But $message looks like:
line 1 line 2 line 3 etc

Thanks for any help

jscheuer1
09-17-2014, 04:06 PM
You can try using the wrap="hard" attribute on the textarea tag. If that doesn't take care of it, you will need to address it on the server side.

mcolton
09-17-2014, 05:03 PM
Thanks. I tried wrap = "hard" and it didn't work.
What do you mean by addressing it on the server side

jscheuer1
09-17-2014, 05:16 PM
Well, you said "$message in the php". You will have to either massage that value to get what you want (change linebreaks to <br> tags perhaps), or use a different type of formatting in the email so that it recognizes the linebreaks as linebreaks.

By way of further explanation, regardless of wrap on the textarea, if the user hits return to create a new line, that linebreak will be sent to the server when the form is submitted. If wrap is set to hard though, even if the linebreak is only created because the text was forced to wrap due to the width of the textarea, those linebreaks will be preserved when submitted.

Problems can arise once the server gets the value if it uses that value in a way (like HTML formatting) where linebreaks are not respected as such, and/or if the server gets the value by some other method than submit (like AJAX), where linebreaks may or may not be preserved due to coding other than the wrap attribute on the textarea tag.

If you want more help, please provide a link to a page on your site that demonstrates the problem. We may also need to see the PHP code that you are using.

jscheuer1
09-17-2014, 05:17 PM
Here's a little demo that shows that the text wrap is preserved (wrap.php):


<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<textarea name="test" cols="50" rows="5" wrap="hard"></textarea><br>
<input type="submit" value="Go!">
</form>
<pre><?php
if(isset($_REQUEST['test'])){
echo $_REQUEST['test'];
}
?></pre>
</body>
</html>

jscheuer1
09-17-2014, 06:44 PM
If the problem is that the email is HTML formatted for example, you can use nl2br:

http://us1.php.net/manual/en/function.nl2br.php

mcolton
09-17-2014, 07:33 PM
This code is not online. I am in the testing stages to see if this even works. 2 files shown below test-email.htm and test-email2.php
Thanks



<HTML>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>League Signup</title>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />

<style type="text/css">
label {
color: black;
font-family: arial;
font-size: 22px;
}
input {
background-color: #ffffcc;
font-family: arial;
font-size: 22px;
border: 1px;
color: blue;
}
button {
color: blue;
font-size: 22px;
height: 32px;
width: 200px;
margin-left: 8%;
}
</style>
</head>

<body>
<form id="emailblast" method="post" action="test-email2.php">
<fieldset style="border:0px">
<p>
<label>Enter your Email Address:</label>
<input type="text" id="xemail" name="xemail" size="40" maxlength="40" required="required" />
</p>
<p>
<label>Enter the Subject of your email:</label>
<input type="text" id="xsubject" name="xsubject" size="40" maxlength="40" required="required" />
</p>
<p>
<label>Enter the Message of your email:</label>
</p>
<p>
<textarea id="xmessage" name="xmessage" rows="20" cols="40" wrap="hard" required="required" /></textarea>
</p>
<p>
<button type="submit" class="button">Submit</button>
</p>
</fieldset>
</form>
</body>
</html>


test-email2.php


<?php
$myemail = filter_input(INPUT_POST, "xemail");
$subject = filter_input(INPUT_POST, "xsubject");
$message = filter_input(INPUT_POST, "xmessage");

try
{
$connect = new PDO('mysql:host=localhost;dbname=xxxx', "xxxx", "xxxx");
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sth = $connect->prepare("SELECT email FROM contacts WHERE lastname LIKE 'Col%' ");
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_COLUMN, 0);

$to = implode(", ", $result);

$connect = null;
} // end try

catch(PDOException $e)
{
echo 'ERROR: ' . $e->getMessage();
}

$headers = "From: $myemail\n";
print "$message";

// mail($to,$subject,$message,$headers);
// header("Location: index.htm");
?>

jscheuer1
09-17-2014, 07:51 PM
If you're just going off of the result of this:


print "$message";

on test-email2.php, then you are most likely seeing linebreaks lost because they will not show up in a browser. They would if you use the browser's 'view source' to see the underlying computed text/code of the page.

Just for fun, try instead:


print nl2br($message);

mcolton
09-17-2014, 08:11 PM
That worked. Thanks for your help