我正在尝试插入数据,但是我正在从脚本ajax调用中获取致命错误。请帮助我一些。谢谢。用它来进行Wordpress插件开发
include_once($_SERVER['DOCUMENT_ROOT'].'/wordpress/wp-config.php' );
global $wpdb;
$table ='wp_mnggallery';
if(isset($_POST['img'])){
$img = $_POST['img'];
$back_image = $_POST['back_img'];
$url = $_POST['url'];
$data = array(
'id' => "",
'images' => "$img",
'back_image' => "$back_image",
'url'=>"$url"
);
$number_images = count($img);
//Inserting the data
$q = $wpdb->insert($table ,$data);
echo $img.' is updated succefully <br>';
}
Ajax调用此代码从另一个页面发送:
<script>
$('#submit').submit(function(event){
event.preventDefault();
var img = $('#img').val();
var backimage = $('#back_img').val();
if(img!=''&backimage!=''){
$.ajax({
type: "POST",
url: '<?php echo plugins_url(); ?>/mngimageeffects/post.php',
data: $('#submit').serialize(),
success: function( response ) {
$('#error').html( response );
$('#mng_img').append('<div class="col-md-4"><img src="'+img+'"width="100%"/></div><div class="col-md-2"><a href="<?php $_SERVER['PHP_SELF']; ?>"><button class="btn btn-primary delete" id="delete">Upload</button></a></div>');
//delete = delete + 1;
$('#img').val('');
$('#back_img').val('');
$('#url').val('');
}
});
}else{
alert('Please Enter The Frent Image and Back Images Urls: ');
}
});
</script>
答案 0 :(得分:0)
当你想在WordPress之外使用WordPress功能时,你不应该包含配置文件但是wp-load.php
define( 'SHORTINIT', true );
require( '/path/to/wp-load.php' );
// use $wpdb after this
答案 1 :(得分:0)
在Wordpress插件中使用AJAX
jQuery('#submit').submit(function(event){
event.preventDefault();
var img = jQuery('#img').val();
var backimage = jQuery('#back_img').val();
var url = jQuery('#url').val();
var data = {
action: 'submit_data',
img: img,
back_img: backimage,
url : url,
};
if(img!=''&backimage!=''){
jQuery.ajax({
type: "POST",
url: ajaxurl, // if its front end else use else ajaxurl
data:data,
dataType: 'html',
success: function( response ) {
alert(response+'sdfs')
jQuery('#error').html( response );
jQuery('#mng_img').append('<div class="col-md-4"><img src="'+img+'"width="100%"/></div><div class="col-md-2"><a href="<?php $_SERVER['PHP_SELF']; ?>"><button class="btn btn-primary delete" id="delete">Upload</button></a></div>'); //delete = delete + 1;
jQuery('#img').val(''); jQuery('#back_img').val(''); jQuery('#url').val('');
},
error : function(data){
alert('Error while deleting.');
return false;
}
});
}
else{ alert('Please Enter The Frent Image and Back Images Urls: '); }
});
在PHP方面,
add_action( 'wp_ajax_my_action', 'submit_data' ); // excutes for users that are logged in.
add_action( 'wp_ajax_nopriv_submit_data', 'submit_data' ); //executes for users that are not logged in.
function submit_data(){
global $wpdb;
$table ='wp_mnggallery';
if(isset($_POST['img'])){
$img = $_POST['img'];
$back_image = $_POST['back_img'];
$url = $_POST['url'];
$data = array(
'id' => "",
'images' => "$img",
'back_image' => "$back_image",
'url'=>"$url"
);
$number_images = count($img);
//Inserting the data
$q = $wpdb->insert($table ,$data);
echo $img.' is updated succefully <br>';
wp_die();
}
}
网址应为网址:admin_url( 'admin-ajax.php' )
,如果其前端使用ajaxurl
,则会自动包含必要的文件