title: Sending out e-mail and updating table with AJAX in WordPress
body: <p>I'm currently making a WordPress plugin where I want to be able to send e-mail to people from a list. I have made the form (id: campaign), and all checkboxes seems to work. The form submit to itself with a query string, so in the same file I have a if-then that check if this query string has some data.</p>
  
  <p>I have made an admin page with a sub form underneath that lists all e-mail addresses:</p>
  
  <p><img src="https://i.stack.imgur.com/LaCbN.jpg" alt="enter image description here"></p>
  
  <p>I want the script to send out e-mails to each e-mail that has been selected, updating the MySQL table that the mail has been sent, and showing a counter on the page (i.e. '#/15 e-mails sent'). I know how to make this script in plain PHP, but my lack of knowledge with AJAX is the problem.</p>
  
  <p>So this is what I want:</p>
  
  <ol>
  <li>The form sending data to itself (same PHP file) - this is working.</li>
  <li>Starting the AJAX script based on the selected checkboxes</li>
  <li>Sending out e-mails and updating the SQL table based on a timer (to avoid spam)</li>
  <li>Showing real-time counter</li>
  </ol>
  
  <p>This is what I have so far:</p>
  
  <pre><code>    &lt;div id="feedback"&gt;&lt;/div&gt;
      &lt;?php
  
  
      add_action('wp_ajax_sendPromo', 'sendPromo');
      add_action('wp_ajax_nopriv_sendPromo', 'sendPromo'); // not really needed
  
      ?&gt;
  
      &lt;script type="text/javascript"&gt;
      jQuery('#campaign').submit(ajaxSubmit);
  
      function ajaxSubmit(){
  
          var campaign = jQuery(this).serialize();
  
          jQuery.ajax({
              type:"POST",
              url: "/wp-admin/admin-ajax.php",
              data: campaign,
              success:function(data){
                  jQuery("#feedback").html(data);
              }
          });
  
          return false;
      }
      &lt;/script&gt;
      &lt;?php
  
      function sendPromo(){
  
      global $wpdb;
  
      $recepients = $_GET['recepients_email']; // array of checkboxes
      for($i=0; $i &lt; count($recepients); $i++){
          mail($recepients[$i], $subject, $message, $headers); // mail is being sent
          if($wpdb-&gt;update('wp_ap_promo',array(
          'sent'=&gt;1
          ), array( 'email' =&gt; '" . $recepients[$i] . "' ))===FALSE){
  
              echo "Error";
  
          }
          else {
              echo "Successfully sent to '".$recepients[$i]. "', row ID is ".$wpdb-&gt;update_id;
  
          }
      }
      die();
  }
  </code></pre>
  
  <p>One of the problems seems to be that the JS isn't being run because the page is reloading (it is meant to reload, because I want the user to see a plain page with the send counter.</p>