Send emails based on a filtered list

Send emails based on a filtered list

patricknypatrickny Posts: 14Questions: 6Answers: 0

I'd like to be able to send emails based on a search completed in Datatables. I use a json array and serverside processing to create the table. The table creates a filtered list (Showing 1 to 35 of 35 entries (filtered from 60 total entries)), and I'd like to be able to send an email to those 35 people. I'm very new to ajax and javascript, but have some experience in php. Is this possible?

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,112Questions: 1Answers: 2,583
    Answer ✓

    Hi @patrickny ,

    Yep , you could create a custom button, which when pressed, uses rows().data() with options along the lines of rows({search:'applied'}).data().pluck(YOUR_EMAIL_COLUMN), and send that back via Ajax to your PHP script to send the messages.

    Cheers,

    Colin

  • patricknypatrickny Posts: 14Questions: 6Answers: 0

    Good Morning Colin, Thank you for the suggestion. As I said, I'm very new at this, but you gave me a direction to go in! Thank you for your help.

  • patricknypatrickny Posts: 14Questions: 6Answers: 0
    edited February 2019

    I'm having a tough time. The JS for my datatable is as follows. test.php provides a json array (?) that fills the table, and makes it sortable and searchable.

            $(document).ready(function(){
              table = $('#datatable').DataTable( {
                "iDisplayLength": 50,
                "processing": true,
                "serverSide": true,
                "sPaginationType": "full_numbers",
                "deferRender": true,
                "aLengthMenu": [[50, 100, 200, 300, 500], [50, 100, 200, 300, 500]],
                "ajax": {
                  "url": "test.php",
                  "type": "GET"
                }
              });
    

    There is a custom-search feature that does work correctly:

              $('#custom-search').detach().insertBefore($('#datatable_filter')).show();
              $('#datatable_filter').css("display","none");
              $("#custom-search input[type=text]").on("keyup", function(e){
                var searchStr = this.value;
                var match = searchStr.match(/^<regex([\d;]+)?>/);
                var regex = match != null;
                if(regex) searchStr = searchStr.replace(/^(<.+>)/,"");
    
                if(searchStr.length > 0 && !regex) {
                  if(e.keyCode == 13) {
                    clearTimeout(timer);
                    table.search(customSearch(searchStr),regex).draw();
                  } else {
                    clearTimeout(timer);
                    timer = setTimeout($.proxy(function(){
                      table.search(customSearch(searchStr),regex).draw();
                    }, this), searchTimeout);
                  }
                } else if(regex) {
                  if(e.keyCode == 13) {
                    var mtch = match[1];
                    var columns = [];
                    if(mtch != undefined) match[1].split(";");
    
                    for(var y = 0; y < columns.length; y++) {
                      columns[y] = parseInt(columns[y]);
                    }
    
                    for(var x = 0; x < 6; x++) {
                      var exists = $.inArray(x, columns);
                      if(exists > -1) table.column(x).search(customSearch(searchStr), regex);
                      else table.column(x).search("", false);
                    }
    
                    table.search(customSearch(searchStr), regex).draw();
                  }
                }
              });
    

    There is also an email function, but I cant figure out how to make it work correctly::

    $("#send-email").on("click", function(){
                            var emails = [];
                            var data = [];
                            var dt = table.ajax.params();
                            var form = $("#compose-form");
                            form.find("input[name=ids]").val(data.join(","));
                            $("#to-list").text(emails.join(", "));
    
                            var body = form.find("textarea[name=bodytxt]").first();
                            if(body.val() == "") {
                                body.focus();
                                return;
                            }
    
                            var subject = form.find("input[name=subj]").first();
                            if(subject.val() == "") {
                                subject.focus();
                                return;
                            }
    
                            var fdata = new FormData($(form)[0]);
                            var more = $.param(dt);
                            var moreDecoded = decodeURIComponent(more).split("&");
                            for(var i = 0; i < moreDecoded.length; i++) {
                                var val = moreDecoded[i].split("=");
                                fdata.append(val[0], val[1]);
                            }
    
                            $.ajax({
                                "url": "email.php",
                                "method": "POST",
                                "contentType": false,
                                "processData": false,
                                "data": fdata,
                                "dataType": "json"
                            }).done(function(data){
    
                            console.log("test");
                                if(data.status == "success") {
                                    $.gritter.add({
                                title: 'Emails sent successfully',
                                text: 'You sent ' + data.totalsent + ' emails to the list of directors in your search criteria.',
                                sticky: false,
                                time: 6000
                              });
                                } else {
                                    $.gritter.add({
                                title: 'Email failure',
                                text: 'Something went wrong and we failed to send your emails.',
                                sticky: false,
                                time: 6000
                              });
                                }
    
                                $("#myModal").modal("hide");
                            }).fail(function(){
                                $("#myModal").modal("hide");
                            });
                        });
            });
    
    

    I tried making an 'export' button that would hopefully bring me one step closer, but I couldn't figure out how to make it work:

      $('#export').click(function () {
          var rows = table.rows( 0 ).data();
        $.ajax({
        url     : 'emget.php',
        type    : 'post',
        data    : rows({search:'applied'}).data().pluck(7),
        dataType: 'json',
    
    });
    });
    

    Any help that you could provide would be increditble!

This discussion has been closed.