jQuery публиковать данные и отправлять электронную почту, когда пользователь нажимает кнопку - PullRequest
0 голосов
/ 12 октября 2019

HTML

<button type="button" class="btn btn-ligt selection" id="Amazon" data-toggle="modal" data-target="#AmazonmodalElem">Select</button>

<script>
    $(document).ready(function() {
        $(".selection").click(function() {
            var myID = $(this).attr('id')
            alert("you clicked the selection: " + myID);

            //start AJAX Post request to post data to the server

            //end of post request

            $.post("post.php", {
                selection: myID
            }, function(data,status){

            $("#message").html(data);
        } );
        //end of AJAX post method

      });
  //end of click event for button selections
  });

</script>

PHP

  <?php
      $mailTo = "admin@google.com";
      $mailFrom = "no-reply@admin.com";
      $subject = "$_POST["subject"]";
      $message = "someone selected a gift card.";

      mail($mailTo, $subject, $message, "From: ".$mailFrom);
  ?>

Обновление: теперь я получил PHP для отправки электронной почты и, похоже, работает нормально. Теперь сообщение об успешном выполнении AJAX не отображается в моем HTML-элементе с сообщением ID ->. Может кто-нибудь дать мне указание на то, что может отсутствовать в моем коде jQuery?

Спасибо!

1 Ответ

0 голосов
/ 12 октября 2019

Попробуйте сейчас и увидите сообщение об успешной печати ajax, как вы просили ...

<script 
  src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> 
  type="text/javascript" charset="utf-8"></script>



<script>

$(document).ready(function(){
  $(".selection).click(function(){  
var myID = $(this).attr('id');
alert("you clicked the selection: " + myID);
var datasend = "myID="+ myID;

        $.ajax({

            type:'POST',
            url:'post.php',
            data:datasend,
                        crossDomain: true,
            cache:false,
            success:function(msg){
            alert(msg); 

                $('#message').fadeIn('slow').prepend(msg);

            }


        });


});

});




</script>


 <body>
<div id="message"> </div>


<button type="button" class="btn btn-ligt selection" id="Amazon" data-toggle="modal" data-target="#AmazonmodalElem">Select</button>


</body>

php

 <?php
      $mailTo = "admin@google.com";
      $mailFrom = "no-reply@admin.com";
     // $subject = $_POST['subject'];
$subject = 'my subject head';
      $message = "someone selected a gift card.";

      mail($mailTo, $subject, $message, "From: ".$mailFrom);

     echo "Email successfully sent";
  ?>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...