how to apply bulk actions in wordpress plugin

I am developing a wordpress plugin in which i have to apply bulk actions. In my plugin there is a table which prints a list of various users. Each row has a checkbox. I want to apply bulk action to the rows which are checked.

<form action= "" method="post" onclick="CheckIfChecked()">             
<select name="select_bulk_action" id="select_bulk" >
<option value="-1" selected="selected">Bulk Actions</option>
 <option value="1">Approve for Mobile</option>
<option value="2">Unapproved for Mobile</option>
</select>
<input type='submit' name ='bulk_action' id="bulk" value='Apply' class ='button-primary' >

Read More

My code for getting rows which are checked is :

global $wpdb; 
$info = $wpdb->get_results("SELECT ID, user_login, user_email, user_registered from $wpdb->users");
foreach ($info as $key=>$infos)             
{ 
$beats123= $wpdb->get_var("SELECT count(post_title) from $wpdb->posts WHERE post_author='$infos->ID' AND post_type='download'");
$mobile_approved1= $wpdb->get_var("SELECT meta_value from $wpdb->usermeta WHERE meta_key='mobile_producer' AND user_id='$infos->ID');
{
echo "<td><form method='post' action=''><input type='checkbox' class='case' name='check_list[]' value='$infos->ID' onclick='CheckIfChecked()'></td>
<td>$infos->ID</td>
<td><a href='http://localhost/nga/wp-admin/admin.php-beat-id=$infos->ID' id='$infos->ID'>$infos->user_login</a></td>
<td>$infos->user_email</td>
<td>$infos->user_registered</td>
<td style='color:deepskyblue'><a href='http://localhost/nga/wp-admin/admin.php?page=my-submenu-slug&user-beat-id=$infos->ID' id='$infos->ID'>$beats123</td></a>
<td align='center' style='color:deepskyblue'>$mobile_approved1</td><td><input type='submit' value='Yes' name='Yes'><input type='hidden' name='userid' value='$infos->ID'>
<input type='submit' value='No' name='No' ></form></td>
<td>yes</td></td>"; 

“Onclick” is a javascript function which is called on a button click and when checkboxes are checked. This method makes an AJAX request to send the data to my PHP function

function CheckIfChecked()
{
var chk = document.getElementsByName("check_list[]"); 
var chklength = chk.length;
var e = document.getElementById("select_bulk"); 
var strUser = e.options[e.selectedIndex].value;
for(k=0;k< chklength;k++)
{
 if (chk[k].checked )
{
 strChoices += " " + chk[k].value + "n"
} 
 } 

if (strChoices.length > 0 && strUser=="1")
{
 if(document.getElementById("bulk").value !="")
{
 $.ajax({
url: ajaxurl,
data: {'data' : strChoices 
action: 'my_check_function'},



 success: function(data, textStatus, XMLHttpRequest) {
        alert(textStatus);        },
    });

} 
}
else if (strChoices.length > 0 && strUser=="2") 
{
 if(document.getElementById("bulk").value !="")
 {
 $.ajax({
 data: {'data' : strChoices
action: 'my_check_function'}, 
success: function(data, textStatus, XMLHttpRequest) {
        alert(textStatus);        },
});

 }
  }

 }


 function my_check_function(){
$data = $_POST['data'];
echo $data;
}

For some reason the AJAX request is not working and the function my_check_function doesn’t get the values I posted.

Any help will be greatly appreciated.

Related posts