我在Wordpress插件中使用jQuery的可排序函数,允许用户设置多个列表项的显示顺序。当用户重新排序项时,jquery .ajax()应该向处理脚本发送请求以更新表。
这是jQuery:
jQuery("#sortable").sortable({
update : function () {
var serial = jQuery('#sortable').sortable('serialize'); //split up each li into an array item
jQuery("body").css("cursor", "progress");
jQuery.ajax({
url: "../wp-content/plugins/libhours/includes/_process.php",
type: "post",
data: serial,
success: function(msg){
jQuery("body").css("cursor", "default");
},
error: function(){
alert("Error updating the table via AJAX.");
jQuery("body").css("cursor", "default");
}
})
}
});
找到了URL,一切正常,直到我从_process.php文件中收到错误
Call to a member function update() on a non-object in ... _process.php
如果我通过表单提交访问_process.php文件,我不会遇到此问题;插入和更新记录工作得很好。这是更新函数供参考......
function cp_libhours_update_area_order($data) {
global $wpdb, $table_prefix;
$i = 1; //our counter to set the rank of each record NOTE: start at 1...0 will be reserved for new entries
foreach ($data as $area) {
// Execute statement:
$update_sql = $wpdb->update($table_prefix . library_areas,
array(
'displayOrder' => $i
),
array(
'id' => $area
)
);
$i++;
}
}
if(isset($_POST['area'])) cp_libhours_update_area_order($_POST['area']);
答案 0 :(得分:1)
这不是在WordPress中进行AJAX调用的方法。您当前正在对_process.php文件进行直接的AJAX调用。该调用将起作用,但它未在WordPress范围内加载。有两种解决方案:
1)在_process.php文件中包含wp-load.php,或者:
2)(最佳选择)执行the WordPress way:使用add_action定义您的操作(' wp_ajax_my_action',' my_action_callback')并对Wordpress执行AJAX调用&# 39; admin_ajax文件。有关详细信息,请查看此页面:http://codex.wordpress.org/AJAX_in_Plugins