我正在創建一個WordPress插件,並且我有一個奇怪的問題。Ajax在WordPress插件中返回問題
首先,我喜歡展示我的WordPress腳本。
<?php
/*
Plugin Name: Banner Plugin
*/
define('MYPLUGINNAME_PATH', plugin_dir_path(__FILE__));
register_activation_hook(__FILE__, 'my_banner_plugin_activate');
register_deactivation_hook(__FILE__, 'my_banner_plugin_deactivate');
function my_banner_plugin_activate(){
//Activation method.
activate();
}
function my_banner_plugin_deactivate(){
//Deactivation method.
global $wpdb;
$wpdb->query(" DROP TABLE ".$wpdb->prefix."banner ");
}
add_action('admin_menu', 'register_custom_menu_page');
function register_custom_menu_page() {
add_menu_page('Banner Menu Display', 'Banner Menu', 'read', 'banner-menu', 'display_banner_panel' /*, $icon_url, $position*/);
add_submenu_page('banner-menu', 'Add Baner', 'Add Baner','read', 'add-banner','display_banner_subpanel_add');
}
if(isset($_POST['action']) && $_POST['action']=='add_banner'){
submit_form();
}
if(isset($_POST['action']) && $_POST['action']=='updateRecordsListings')
{
include 'reorder.php';
}
function header_css_page() {
wp_register_style($handle = 'header-css', $src = plugins_url('css/banner_style.css', __FILE__), $deps = array(), $ver = '1.0.0', $media = 'all');
wp_enqueue_style('header-css');
}
add_action('init', 'header_css_page');
function reorder_css_page() {
wp_register_style($handle = 'reorder-css', $src = plugins_url('css/reorder_style.css', __FILE__), $deps = array(), $ver = '1.0.0', $media = 'all');
wp_enqueue_style('reorder-css');
}
add_action('init', 'reorder_css_page');
function display_banner_panel()
{
global $wpdb;
$banner_list = $wpdb->get_results("select * from ".$wpdb->prefix."banner order by banner_order desc");
?>
<div class='admin-custom'>
<?php include 'view_banner.php'; ?>
</div>
<?php
}
view_banner.php
的腳本如下;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="<?php echo plugins_url();?>/banner-pluggin/js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="<?php echo plugins_url();?>/banner-pluggin/js/jquery-ui-1.7.1.custom.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(function() {
$("#contentLeft2 ul").sortable({ opacity: 0.6, cursor: 'move', update: function() {
var order = $(this).sortable("serialize") + '&action=updateRecordsListings';
$.post("", order, function(theResponse){
$("#contentRight2").html(theResponse);
});
}
});
});
});
</script>
</head>
<body>
<h1>Add Banner</h1>
<br/>
<br/>
<div id="contentWrap2">
<div id="contentLeft2">
<ul>
<?php
//$query = "SELECT * FROM records ORDER BY recordListingID ASC";
//$result = mysql_query($query);
//while($row = mysql_fetch_array($result, MYSQL_ASSOC))
//{
foreach($banner_list as $row)
{
?>
<li id="recordsArray_<?php echo $row->banner_id; ?>">
<div>
<img src="<?php echo plugins_url().'/banner-pluggin/banner/'. $row->banner_img;?>" height="80px" width="100px"/>
</div>
</li>
<?php } ?>
</ul>
</div>
<div id="contentRight2">
<p>Array will be displayed here.</p>
<p> </p>
</div>
</div>
</body>
</html>
而reorder.php
如下;
<?php
global $wpdb;
$action = mysql_real_escape_string($_POST['action']);
$updateRecordsArray = $_POST['recordsArray'];
if ($action == "updateRecordsListings")
{
$listingCounter = 1;
foreach ($updateRecordsArray as $recordIDValue)
{
$query = "UPDATE wp_banner SET banner_order = " . $listingCounter . " WHERE banner_id = " . $recordIDValue;
if(mysql_query($query))
{
}
else {
mysql_error();
}//die('Error, insert query failed');
$listingCounter = $listingCounter + 1;
}
echo '<pre>';
print_r($updateRecordsArray);
echo '</pre>';
echo 'If you refresh the page, you will see that records will stay just as you modified.';
}
?>
的問題是,AJAX調用也工作正常,唯一的問題是,Ajax響應還顯示管理菜單的一部分,
這裏是截圖:
在右側的綠色框中,管理員的部分不應顯示,但爲什麼顯示?
我知道我想通了這個問題,
$.post("", order, function(theResponse)
使Ajax調用被送到這意味着banner-pluggin.php
和行動後banner-pluggin.php
的HTML是越來越加載Ajax中的同一頁。
但是,如果我直接發送Ajax帖子到reorder.php
,那麼因爲沒有HTML,HTML將不會作爲響應加載。
但隨後另一個問題:SQL查詢不運行,也不顯示有任何錯誤,也沒有SQL被執行