简体   繁体   中英

AJAX send text in only one line

I have a problem with an ajax request.
I have a form that has two text input and a textarea and an ajax function that sends to another page called mail.php the text of the three input, then mail.php sends an e-mail with those data and return 1 if the e-mail was sent correctly and 0 if not. It works but in the arrived e-mail the text is only in one line also if in the textarea i have got 3 or 4 line. This is the form:

<div id="form">
    <input type="email" class="style" id="mail" maxlength="80" placeholder="Inserisci la tua e-mail" required />
    <br>
    <input type="text" class="style" id="subject" maxlength="50" placeholder="Oggetto" required />
    <br>
    <textarea id="message" class="style" placeholder="Scrivi il messaggio..." rows="8" maxlength="2000" required></textarea>
    <br>

    <button type="submit" id="submit_button"><div><p class="send_button_content">Invia</p><img id="send_button_icon" class="send_button_content" src="send_icon.png"></div></button>
</div>

And this is the ajax function:

function sendEmail(){
                var mail;
                var subject;
                var message;
                mail = $("#mail").val();
                subject = $("#subject").val();
                message = $("#message").val();
                if (mail !="" && subject !="" && message !="") {
                    if (emailCheck(mail)){
                   $.post("mail.php",{mail: mail,subject: subject,message: message},function(data){alert(data);});
                   }else{
                        alert("The e-mail is not correct")  
                   }
                } else {
                        alert("Fill all");
                }
            }
                $("#submit_button").click(function(){sendEmail(); });

And mail.php is like this:

$mail = $_POST['mail'];
$subject = "Nuovo messaggio: ".$_POST['subject'];
$message = "<html>
        <head>
        </head>
        <body>
        <p>New message from: ".$mail."<br>
        Message: ".$_POST['message']."<br>
        <a href='mailto:".$mail."?subject=".$_POST['subject']."' style='color:blue' target='_blank'>Reply here</a>
        </p>
        </body>
        </html>";

if($mail != "" && $subject != "" && $message != ""){
    $to = "***********@gmail.com"; 
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $result = mail($to,$subject,$message,$headers) or die('0');
    if(result){
        die('1');
    }else{
        die('0');
    }
}else{
    die('0');
}

Is there a method that take the text as I have written and send it?

使用nl2br()

 $message=nl2br($_POST['message']);

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