I am creating a WordPress plugin and I have a strange issue.
First of all I like to show my WordPress script.
<?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
}
The script for view_banner.php
is as follows;
<!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>
And the reorder.php
is as follows;
<?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.';
}
?>
The problem is that the ajax calling is also working fine, the only problem is that the ajax response also displays the part of the admin menus,
here's a screenshot:
On the green box at the right, the portion of the admin shouldn't be shown, but why is it showing up?
I know I figured out the problem,
$.post("", order, function(theResponse)
Causes the Ajax call to be sent to the same page that means banner-pluggin.php
and after the action the html of the banner-pluggin.php
is getting loaded as in Ajax.
But if I directly send the Ajax post to reorder.php
, then since there is no HTML, the HTML won't be loaded as response.
But then another issue: the SQL query doesn't run, neither it shows any error, nor SQL gets executed