Results 1 to 9 of 9

Thread: using textarea for a message in an email

  1. #1
    Join Date
    May 2009
    Location
    Greensboro, GA
    Posts
    163
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default using textarea for a message in an email

    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
    Last edited by mcolton; 09-18-2014 at 10:15 AM.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    May 2009
    Location
    Greensboro, GA
    Posts
    163
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    Thanks. I tried wrap = "hard" and it didn't work.
    What do you mean by addressing it on the server side

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Here's a little demo that shows that the text wrap is preserved (wrap.php):

    PHP Code:
    <!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>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    May 2009
    Location
    Greensboro, GA
    Posts
    163
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    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 Code:
    <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 Code:
    <?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_ERRMODEPDO::ERRMODE_EXCEPTION);

          
    $sth $connect->prepare("SELECT email FROM contacts WHERE lastname LIKE 'Col%' ");
          
    $sth->execute();
          
    $result $sth->fetchAll(PDO::FETCH_COLUMN0);
                
          
    $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");
    ?>
    Last edited by jscheuer1; 09-17-2014 at 07:53 PM. Reason: format code

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    If you're just going off of the result of this:

    PHP Code:
    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:

    PHP Code:
    print nl2br($message); 
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    May 2009
    Location
    Greensboro, GA
    Posts
    163
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    That worked. Thanks for your help

Similar Threads

  1. Replies: 1
    Last Post: 01-04-2012, 11:06 PM
  2. Replies: 1
    Last Post: 07-11-2010, 03:26 PM
  3. Replies: 2
    Last Post: 05-28-2008, 04:16 AM
  4. Replies: 2
    Last Post: 11-10-2006, 05:22 PM
  5. Replies: 1
    Last Post: 05-30-2006, 05:08 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •