简体   繁体   中英

How to put paragraphs inside PHP contact form?

I programmed a contact form in PHP and I want to include paragraphs in the message that gets send, so that the user would have to write in certain paragraphs and not just one block of text.

I've tried to split the message into multiple textareas , but I don't know how I should adjust the PHP code so that these textareas would get send inside one message in the right order. Each textarea should've presented one paragraph.

This is my PHP:

<?php 
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: mywebsite.com'; 
    $to = 'myemailadress@adress.com'; 
    $subject = 'subject line';
    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html\r\n";
    $headers .= 'From: '. $email. "\r\n" .
    $headers .= "Reply-To: ". $email. "\r\n";
    $headers .= "X-Mailer: PHP/" . phpversion();

    $status = mail($to, $subject, $message, $headers);

    if ($status) { 
        echo '<p style="color: white">Your Message was sent!</p>';
    } else { 
        echo '<p style="color: white">Something went wrong. Please try again.</p>'; 
    }
?>

This is my HTML:

<div class="contact-form">
    <form id="contact-form" method="post" action="contact-form-handler.php">
        <input name="name" type="text" class="form-control" placeholder="Your Name" required>
        <br>
        <input name="email" type="email" class="form-control" placeholder="Your Email">
        <br>
        <textarea name="message" class="form-control" placeholder="Message" rows="30" required></textarea><br>
        <input type="submit" class="form-control" value="SEND MESSAGE">
    </form>
</div>

I have add:

$message = nl2br($message); // Inserts HTML line breaks <br />

this should Insert HTML line breaks.

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: mywebsite.com';
$to = 'myemailadress@adress.com';
$subject = 'subject line';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";

$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$headers .= 'From: ' . $email . "\r\n" .
$headers .= 'Reply-To: ' . $email . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();

$message = nl2br($message); // <-- added to Inserts HTML line breaks.

$status = mail($to, $subject, nl2br($message), $headers);

if ($status) {
    echo '<p style="color: white">Your Message was sent!</p>';
} else {
    echo '<p style="color: white">Something went wrong. Please try again.</p>';
}

https://www.php.net/manual/en/function.nl2br.php

When you use nl2br you can change the "\\n" from the textarea to a "
". You can send a content-type: text/plain mail to and don't have to transform the newline characters into
tags.

I don't see multiple textareas in your code so i guess nl2br is the function you're looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM