我正在开发一个 wordpress插件,其中我必须应用批量操作。在我的插件中有一个表打印各种用户的列表。每行都有一个复选框。我想将批量操作应用于已检查的行。
<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' >
我获取检查行的代码是:
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>";
&#34; ONCLICK&#34;是一个javascript函数,在按钮单击时调用,并在选中复选框时调用。此方法发出AJAX请求以将数据发送到我的PHP函数
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;
}
由于某种原因,AJAX请求无效,函数my_check_function无法获取我发布的值。
非常感谢任何帮助。