Jump to content

PHP form - remove \r\n


Gainax

Recommended Posts

I have a form, which takes the input and when submitted it sends an email to me with the content.

 

One field is a textarea and when I get the email I get \r\n where someone has gone onto a newline in the text area.

 

My form is:

 

           <form  id="requestform" name="requestform" method="post" action="">

                                <p><label for="name">Name:*</label><br />
                                <?php echo (($nameOK) ? "" : "<p style=\"color:red;\"><strong>Invalid Name: </strong> Your name must be at least 3 letters<br /></p>"); ?>
                                <input name="name" class="text" type="text" id="name" value="<?=$_POST['name'] ?>" /></p>

                                <p><label for="address">Address:*</label><br />
                                <?php echo (($addressOK) ? "" : "<p style=\"color:red;\"><strong>Invalid Address: </strong> Please enter your full address<br /></p>"); ?>
                                <textarea name="address" class="text" name="address" cols="30" rows="5" value="<?=$_POST['address'] ?>" ></textarea><br /></p>
             <button  type="submit" name="send" value="send" style="height: 26px; width: 96px; border: 0px;" ><img src="images/send_request.jpg" alt="send" title=""  /></button>

 

And the code which takes the form data is:

<?php
if( isset($_POST['send']))
{

$nameOK = validateName($_POST['name']);
$addressOK = validateAddress($_POST['address']);


if ($nameOK && $addressOK )
{

                        $to = "[email protected]";
                        $subject = "Request";
                        $body = "Name: " . $name . "\n\n" . "Address: " .$address;

                        $headers = 'From: [email protected]' . "\r\n" .
                        'X-Mailer: PHP/' . phpversion();
                        if (mail($to, $subject, $body, $headers))
                        {
                        header('Location: thankyou.php');
                        }
                        else
                        {

 

I've tried using nl2br around the $address variable, but I still get the \r\n after each linebreak

 

Am I doing anything wrong?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/166646-php-form-remove-rn/
Share on other sites

      function validateAddress($address){
                //if it's NOT valid
                if(strlen($address) < 4)
                        return false;
                //if it's valid
                else
                        return true;
        }

Link to comment
https://forums.phpfreaks.com/topic/166646-php-form-remove-rn/#findComment-878763
Share on other sites

well according to everything you provided which was not much, then your php setup must not support these popular functions. Because trim be default will strip both of the characters you are trying to remove. Or you can post more of your code for error checking. Also you may want to do some htmlspecialchars() to make sure html is not being injected.

Link to comment
https://forums.phpfreaks.com/topic/166646-php-form-remove-rn/#findComment-879598
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.