猿问

Sweetalert2 textarea 输入显示在电子邮件上

我如何设法接收文本区域内用户的输入?以下是我更详细的意思:

https://i.stack.imgur.com/trG1E.gif

我的 JS 看起来像这样:


$('#sad').on('click', function() {

    const sad    = $('#sad').val(); 

    const { value: text } = Swal.fire({

        title:'<strong>Lass uns mehr erfahren</strong>',

        input: 'textarea'

    }).then(function() {

    $.ajax({

        type: "POST",

        url: "feedback.php",//===PHP file name and directory====

        data:{sad: sad},

    success:function(data){

        console.log(data);

        //Success Message == 'Title', 'Message body', Last one leave as it is

        Swal.fire({

        icon: 'success',

        title: "Danke!",

        showConfirmButton: false,

        timer: 3000



    });

    },

    error:function(data){

        //Error Message == 'Title', 'Message body', Last one leave as it is

        Swal.fire({

            icon: 'error',

            title: 'Oops...',

            text: 'Something went wrong!'

})

}

});

if (text) {

    $.ajax({

         type: "POST", 

         url: "feedback.php", 

         data: {'text': text}

        })

}

})

})

和我的 PHP:


$sad = $_POST['sad'];

$text = $_POST['text'];

    elseif(isset( $_POST['sad'] )) { 


                    $EmailFrom = "xxxxxxxx@gmail.com";

                    $Subject = "New Feedback";

                    $mailTo = "info@xxxxxxxx.me";

                    $txt = "Oh no, you got a new 'Sad' review".$text;

                    $headers = 'From: ' .$EmailFrom. "\r\n";

                    mail($mailTo, $Subject, $txt, $headers);

                    header("Location: https://xxxxxxxx.me/?mailsend");

                    }

我收到了 $txt 部分,但没有收到每封电子邮件的用户输入:“哦,不,你收到了一条新的‘悲伤’评论”


翻翻过去那场雪
浏览 178回答 1
1回答

墨色风雨

在下面的代码中,我已经在内部传递了 textarea 的值function(result),然后我习惯了result.value访问 textarea 的值并将其传递给 ajax 调用。演示代码:$('#sad').on('click', function() {&nbsp; const sad = $('#sad').val();&nbsp; const {&nbsp; &nbsp; value: text&nbsp; } = Swal.fire({&nbsp; &nbsp; title: '<strong>Lass uns mehr erfahren</strong>',&nbsp; &nbsp; input: 'textarea'&nbsp; }).then(function(result) {&nbsp; //chcking if user have type something in textbox&nbsp; &nbsp; if (result.value) {&nbsp; &nbsp; //printing in console&nbsp; &nbsp; &nbsp; console.log(result.value)&nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; type: "POST",&nbsp; &nbsp; &nbsp; &nbsp; url: "feedback.php", //===PHP file name and directory====&nbsp; &nbsp; &nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sad: sad,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; values: result.value //<-sending textarea value&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; success: function(data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Success Message == 'Title', 'Message body', Last one leave as it is&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Swal.fire({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; icon: 'success',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title: "Danke!",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showConfirmButton: false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timer: 3000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; error: function(data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Error Message == 'Title', 'Message body', Last one leave as it is&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Swal.fire({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; icon: 'error',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title: 'Oops...',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: 'Something went wrong!'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; if (text) {&nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; type: "POST",&nbsp; &nbsp; &nbsp; &nbsp; url: "feedback.php",&nbsp; &nbsp; &nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'text': text&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }&nbsp; })})<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script><button id="sad">Click</button>然后,您需要使用 php 获取在 ajax 中传递的值$_POST['values']。
随时随地看视频慕课网APP
我要回答