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.
I have made an admin page with a sub form underneath that lists all e-mail addresses:
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.
So this is what I want:
This is what I have so far:
<div id="feedback"></div>
<?php
add_action('wp_ajax_sendPromo', 'sendPromo');
add_action('wp_ajax_nopriv_sendPromo', 'sendPromo'); // not really needed
?>
<script type="text/javascript">
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;
}
</script>
<?php
function sendPromo(){
global $wpdb;
$recepients = $_GET['recepients_email']; // array of checkboxes
for($i=0; $i < count($recepients); $i++){
mail($recepients[$i], $subject, $message, $headers); // mail is being sent
if($wpdb->update('wp_ap_promo',array(
'sent'=>1
), array( 'email' => '" . $recepients[$i] . "' ))===FALSE){
echo "Error";
}
else {
echo "Successfully sent to '".$recepients[$i]. "', row ID is ".$wpdb->update_id;
}
}
die();
}
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.