这是我的代码,它没有wordpress。但是随着wordpress的推出,ajax部分总是失败。 我在wordpress中包含了jquery和ajax,就像这样
function transtatus_metaboxes(){
if (is_admin() && !trans_parents_zero($GLOBALS["post_info"])){
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', 'http://code.jquery.com/jquery-latest.js');
wp_enqueue_script( 'jquery' );
add_meta_box('transtatus_show', 'Translations Status', 'transtatus_show', 'post', 'side', 'high');
}
}
function trans_action_ajax(){
?><!-- jquery ajax function -->
<script type="text/javascript">
<!--
$(function() {
$(".ovalbutton").click(function() {
var val = $(this).attr('href').match(/p=([0-9]+)/)[1];
var val_adi = $(this).attr('href').match(/id=([0-9]*)/)[1];
var str_val = null;
var fnl_val = null;
var dataString = null;
if(val == 1){
str_val = "Require Translation";
} else if(val == 2) {
str_val = "On Progress";
} else if(val == 3) {
str_val = "Completed";
} else {
str_val = "Finialised";
}
fnl_val = str_val + "," + val_adi;
dataString = 'trans_cont_value=' + fnl_val;
$.ajax({
type: "POST",
url: "http://localhost/marcopolowordpress/wp-content/plugins/translation-status/update.php",
data: dataString,
cache: false,
success: function(html){
$('#display').text(" status set to " + str_val);
}
});
alert(dataString);
});
});
//->
</script>
然后这是update.php
<?php
require_once(dirname(dirname(dirname(dirname(__FILE__))))."/wp-config.php");
$q=$_POST["trans_cont_value"];
$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_query("set names ".DB_CHARSET); //solved the unwanted character
mysql_select_db(DB_NAME, $con);
if(isset($q)){
$strarray = explode(',', $q);
$value = $strarray[0];
$id = $strarray[1];
mysql_query("UPDATE MESSAGES SET msg = '$value'");
}
?>
我没有看到这些代码有任何问题,但它在wordpress中不起作用。任何帮助? 提前欣赏
答案 0 :(得分:1)
您必须更改网址
url: "http://localhost/marcopolowordpress/wp-content/plugins/translation-status/update.php",
to
url: "<?php get_bloginfo('wpurl');?>/wp-content/plugins/translation-status/update.php",