Sweetalert2 ввода текстового поля для отображения в электронной почте - PullRequest
1 голос
/ 08 мая 2020

Как мне удается получать вводимые пользователем данные внутри текстового поля? Ниже я имею в виду более подробно:

enter image description here

my 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}
        })
}
})
})

и my 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, но не пользовательский ввод по электронной почте: «О нет, у вас есть новый« грустный »обзор»

1 Ответ

1 голос
/ 08 мая 2020

В приведенном ниже коде я передал значение textarea внутри function(result), а затем использовал result.value для доступа к значению textarea и передал его в вызов ajax.

Demo Код :

$('#sad').on('click', function() {
  const sad = $('#sad').val();
  const {
    value: text
  } = Swal.fire({
    title: '<strong>Lass uns mehr erfahren</strong>',
    input: 'textarea'
  }).then(function(result) {
  //chcking if user have type something in textbox
    if (result.value) {
    //printing in console
      console.log(result.value)
      $.ajax({
        type: "POST",
        url: "feedback.php", //===PHP file name and directory====
        data: {
          sad: sad,
          values: result.value //<-sending textarea value
        },
        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
        }
      })
    }
  })
})
<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>

Затем вам нужно получить значение, которое передается в ajax, используя $_POST['values'] в php.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...