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 = "example@example.com";
                        $subject = "Request";
                        $body = "Name: " . $name . "\n\n" . "Address: " .$address;

                        $headers = 'From: example@example.co.uk' . "\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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.