我试图在我的WordPress插件中使用ajax方法从数据库表中检索元组
这是我的专辑插件脚本
<?php
/*
Plugin Name: Album Pluggin
*/
define( 'MYPLUGINNAME_PATH', plugin_dir_path(__FILE__) );
register_activation_hook( __FILE__, 'my_album_pluggin_activate' );
register_deactivation_hook( __FILE__, 'my_album_pluggin_deactivate' );
function my_album_pluggin_activate(){
//Activation method.
activate_album_pluggin();
}
function my_album_pluggin_deactivate(){
//Deactivation method.
global $wpdb;
$wpdb->query(" DROP TABLE ".$wpdb->prefix."album ");
$wpdb->query(" DROP TABLE ".$wpdb->prefix."image ");
}
add_action( 'admin_footer', 'my_action_javascript' );
function my_action_javascript() {
?>
<script type="text/javascript" >
function get_album_data(album_id)
{
alert('album_id is '+album_id);
var data = {
action: 'my_action',
album_id: album_id
};
$.post(ajaxurl,data,
function(res)
{
var message_result = eval('(' + res + ')');
alert(message_result.album_name);
}
);
}
</script>
<?php
}
add_action('wp_ajax_my_action', 'my_action_callback');
function my_action_callback() {
global $wpdb;
$album_id=$_POST['album_id'];
$res = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."album WHERE album_id='$album_id'");
echo json_encode($res);
die();
}
add_action('admin_menu', 'register_custom_album_menu_page');
function register_custom_album_menu_page() {
add_menu_page( 'Album Menu Display', 'Album Menu', 'read', 'album-menu', 'display_album_panel' /*, $icon_url, $position*/ );
add_submenu_page( 'album-menu', 'Add Album', 'Add Album','read', 'add-album','display_album_subpanel_add');
add_submenu_page( 'album-menu', 'Edit Album', 'Edit Album','read', 'edit-album','display_album_subpanel_edit');
}
if(isset($_POST['action']) && $_POST['action']=='add_album'){
submit_album_form();
}
if(isset($_POST['action']) && $_POST['action']=='updateRecordsListings')
{
include 'reorder.php';
}
function header_album_css_page() {
wp_register_style($handle = 'header-css', $src = plugins_url('css/album_style.css', __FILE__), $deps = array(), $ver = '1.0.0', $media = 'all');
wp_enqueue_style('header-css');
}
add_action('init', 'header_album_css_page');
function reorder_album_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_album_css_page');
function display_album_panel()
{
global $wpdb;
$banner_list = $wpdb->get_results("select * from ".$wpdb->prefix."album order by album_order desc");
?>
<div class='admin-custom'>
<?php include 'view_album.php'; ?>
</div>
<?php
}
function display_album_subpanel_add()
{ ?>
<div class='admin-custom'>
<?php include 'add_album.php'; ?>
</div>
<?php
}
function display_album_subpanel_edit()
{
global $wpdb;
$res = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."album ORDER BY album_order ASC");
?>
<div class='admin-custom'>
<?php include 'edit_album.php'; ?>
</div>
<?php
}
/*add_shortcode('DISPLAY-BANNER-SLIDER','func_display_banner');
function func_display_banner(){
global $wpdb;
$res = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."banner ORDER BY banner_order DESC");
$path = ABSPATH . 'wp-content/plugins/banner-pluggin/banner/';
$html = '<!-- banner slider-->
<div id="header">
<div class="slider-wrapper theme-default">
<div id="slider" class="nivoSlider">';
foreach($res as $row){
$html .='<img src="'.plugins_url().'/banner-pluggin/banner/'. $row->banner_img .'" alt="" title="'.$row->banner_content.'">';
}
$html .= '</div>
</div>
</div>
<!--//slider-->';
return $html;
}*/
function activate_album_pluggin(){
global $wpdb;
/*if(!file_exists(ABSPATH .'wp-content/plugins/banner-pluggin/banner/'))
mkdir(ABSPATH .'wp-content/plugins/banner-pluggin/banner/');*/
$sql_album = "CREATE TABLE IF NOT EXISTS ".$wpdb->prefix."album (
album_id INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
album_name VARCHAR( 255 ) NOT NULL ,
album_slug VARCHAR( 255 ) NOT NULL ,
album_content TEXT NOT NULL ,
album_order INT( 11 ) NOT NULL,
album_timestamp VARCHAR( 20 ) NOT NULL
)";
$sql_image = "CREATE TABLE IF NOT EXISTS ".$wpdb->prefix."image (
image_id INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
image_name VARCHAR( 255 ) NOT NULL ,
image_slug VARCHAR( 255 ) NOT NULL ,
image_img VARCHAR( 255 ) NOT NULL ,
image_content TEXT NOT NULL ,
album_id INT( 11 ) NOT NULL,
image_order INT( 11 ) NOT NULL,
image_status INT( 11 ) NOT NULL,
image_timestamp VARCHAR( 20 ) NOT NULL
)";
$wpdb->query($sql_album);
$wpdb->query($sql_image);
}
function submit_album_form(){
global $wpdb;
$albumname = $_POST['album_name'];
$albumcontent = $_POST['album_content'];
$albumtimestamp = time();
$imagename = $_POST['image_name'];
$wpdb->insert(
$wpdb->prefix."album",
array(
'album_name' => $albumname,
'album_content' => $albumcontent,
'album_timestamp' => $albumtimestamp
),
array(
'%s',
'%s',
'%s'
)
);
$album_info = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."album WHERE album_timestamp='".$albumtimestamp."'");
foreach($album_info as $row)
{
$id=$row->album_id;
}
$albumslug=$id."-".make_slug($albumname);
$update_data=array(
'album_slug'=>$albumslug
);
if(!file_exists(ABSPATH .'wp-content/plugins/album-pluggin/'.$albumslug.'/'))
mkdir(ABSPATH .'wp-content/plugins/album-pluggin/'.$albumslug.'/');
if(move_uploaded_file($_FILES["album_img"]["tmp_name"],ABSPATH . 'wp-content/plugins/album-pluggin/'.$albumslug.'/' . $_FILES["album_img"]["name"]))
echo "done";
else
echo "error";
$wpdb->update(
$wpdb->prefix."album", // Table
$update_data, // Array of key(col) => val(value to update to)
array(
'album_timestamp' => $albumtimestamp
) // Where
);
$image_link='wp-content/plugins/album-pluggin/'.$albumslug.'/'. $_FILES["album_img"]["name"];
$image_timestamp=intval(time()+10);
$wpdb->insert(
$wpdb->prefix."image",
array(
'image_name' => $imagename,
'album_id' => $id,
'image_img'=>$image_link,
'image_timestamp' => $image_timestamp
),
array(
'%s',
'%d',
'%s',
'%s'
)
);
$image_info = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."image WHERE image_timestamp='".$image_timestamp."'");
foreach($image_info as $irow)
{
$i_id=$irow->image_id;
}
$imageslug=$id."-".make_slug($imagename);
$update_image_data=array(
'image_slug'=>$imageslug
);
$wpdb->update(
$wpdb->prefix."image", // Table
$update_image_data, // Array of key(col) => val(value to update to)
array(
'image_timestamp' => $image_timestamp
) // Where
);
}
function make_slug($data)
{
$data_slug = trim($data," ");
$search = array('/','\\',':',';','!','@','#','$','%','^','*','(',')','_','+','=','|','{','}','[',']','"',"'",'<','>',',','?','~','`','&',' ','.');
$data_slug = str_replace($search, "_", $data_slug);
return $data_slug;
}
/* add_action( 'wp_enqueue_scripts', 'prefix_add_my_stylesheet' );
function prefix_add_my_stylesheet() {
wp_register_style( 'custom-style', plugins_url('style.css', __FILE__) );
wp_enqueue_style( 'custom-style' );
}*/
?>
这是我的edit_album脚本
<!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();?>/album-pluggin/js/jquery-1.7.1.min.js"></script>
</head>
<body>
<h1>Add Album</h1>
<form action="" method="post" enctype="multipart/form-data">
<input name="action" type="hidden" value="edit_album"/>
<div class="label_div">
<span class="label_data">Select Album</span>
</div>
<div class="text_div">
<select class="text_data" name="album_id" onchange="get_album_data(this.value);">
<option value="0">Select Album</option>
<?php
foreach($res as $r)
{?>
<option value="<?php echo $r->album_id;?>"><?php echo $r->album_name;?></option>
<?php
}?>
</select>
</div>
<div class="clear_div"></div>
<div class="label_div">
<span class="label_data">Album Name</span>
</div>
<div class="text_div">
<input class="text_data" name="album_name" id="album_name" type="text"/>
</div>
<div class="clear_div"></div>
<div class="label_div">
<span class="label_data">Album Image</span>
</div>
<div class="text_div">
<input name="album_img" type="file"/>
</div>
<div class="clear_div"></div>
<div class="label_div">
<span class="label_data">Image Name</span>
</div>
<div class="text_div">
<input name="image_name" type="text"/>
</div>
<div class="clear_div"></div>
<div class="label_div">
<span class="label_data">Album content</span>
</div>
<div class="text_div">
<textarea class="textarea_data" name="album_content" cols="" rows=""></textarea>
</div>
<div class="clear_div"></div>
<input name="submit" type="submit" value="Add Banner">
</form>
</body>
</html>
问题是脚本部分
function(res)
{
var message_result = eval('(' + res + ')');
alert(message_result.album_name);
}
始终警告未定义。
我想我已经提出了一些关于json编码的问题,或者我可能没有正确解析它。
答案 0 :(得分:0)
好的,因为我正在尝试不同的方法,我从座位中找到了解决方案
我使用以下更改更改了脚本my_action_javascript()
function my_action_javascript() {
?>
<script type="text/javascript" >
function get_album_data(album_id)
{
alert('album_id is '+album_id);
var data = {
action: 'my_action',
album_id: album_id
};
$.post(ajaxurl,data,
function(res)
{
alert(res);
alert(res[0].album_name);
},'json'
);
}
</script>
<?php
}
和宾果我得到了结果,
因为$ res是一个带有单个元组的结果数组。因此第0行将成为我期望结果的载体..