我必须在wordpress中使用ajax将值插入到数据库表wp_ratings表中,但它不起作用。以下是我正在使用的代码:
Ajax电话:
$(function(){
$('#submit').click(function(){
var rating_allowed=document.getElementById("rating_allowed").value;
var product_id=document.getElementById("product_id").value;
var user_id=document.getElementById("user_id").value;
var pc_ip=document.getElementById("pc_ip").value;
var performance_input=document.getElementById("performance_input").value;
var features_input=document.getElementById("features_input").value;
var battery_input=document.getElementById("battery_input").value;
var overall_input=document.getElementById("overall_input").value;
var rating_cat_count=document.getElementById("rating_cat_count").value;
var rate_1_name_find=document.getElementById("rate_1_name_find").value;
var rate_2_name_find=document.getElementById("rate_2_name_find").value;
var rate_3_name_find=document.getElementById("rate_3_name_find").value;
var rate_4_name_find=document.getElementById("rate_4_name_find").value;
var pros=document.getElementById("pros").value;
var cons=document.getElementById("cons").value;
alert("g");
alert("action=updateRating&product_id=" + product_id + "&user_id=" + user_id + "&pc_ip=" + pc_ip + "&performance_input=" + performance_input + "&features_input=" + features_input + "&battery_input=" + battery_input + "&overall_input=" + overall_input + "&rating_cat_count=" + rating_cat_count +"&rate_1_name_find=" + rate_1_name_find + "&rate_2_name_find=" + rate_2_name_find + "&rate_3_name_find=" + rate_3_name_find + "&rate_4_name_find=" + rate_4_name_find + "&pros=" + pros + "&cons=" + cons);
$.ajax({
url:"/wp-admin/admin-ajax.php",
type:'POST',
data:"action=updateRating&product_id=" + product_id + "&user_id=" + user_id + "&pc_ip=" + pc_ip + "&performance_input=" + performance_input + "&features_input=" + features_input + "&battery_input=" + battery_input + "&overall_input=" + overall_input + "&rating_cat_count=" + rating_cat_count +"&rate_1_name_find=" + rate_1_name_find + "&rate_2_name_find=" + rate_2_name_find + "&rate_3_name_find=" + rate_3_name_find + "&rate_4_name_find=" + rate_4_name_find + "&pros=" + pros + "&cons=" + cons,
success:function(results)
{
alert(results);
}
});
}
);
});
在functions.php文件中:
function implement_ajax_updateRating()
{
$p_id = mysql_real_escape_string(htmlentities(trim($_POST['product_id'])));
$user_id = mysql_real_escape_string(htmlentities(trim($_POST['user_id'])));
$pc_ip = mysql_real_escape_string(htmlentities(trim($_POST['pc_ip'])));
$rate_1 = mysql_real_escape_string(htmlentities(trim($_POST['performance_input'])));
$rate_2 = mysql_real_escape_string(htmlentities(trim($_POST['features_input'])));
$rate_3 = mysql_real_escape_string(htmlentities(trim($_POST['battery_input'])));
$rate_4 = mysql_real_escape_string(htmlentities(trim($_POST['overall_input'])));
$rating_cat_numbers = mysql_real_escape_string(htmlentities(trim($_POST['rating_cat_count'])));
$rate_1_find = mysql_real_escape_string(htmlentities(trim($_POST['rate_1_name_find'])));
$rate_2_find = mysql_real_escape_string(htmlentities(trim($_POST['rate_2_name_find'])));
$rate_3_find = mysql_real_escape_string(htmlentities(trim($_POST['rate_3_name_find'])));
$rate_4_find = mysql_real_escape_string(htmlentities(trim($_POST['rate_4_name_find'])));
$pros = mysql_real_escape_string(htmlentities(trim($_POST['pros'])));
$cons = mysql_real_escape_string(htmlentities(trim($_POST['cons'])));
//add_post_meta('17', 'ff_value', "ff", true);
$rating_insert = mysql_query("INSERT INTO wp_rating (post_id, rating_1, rating_2,rating_3,rating_4,user_id,comment_id,pc_ip,pros,cons)
VALUES (".$p_id.", ".$rate_1.", ".$rate_2.",".$rate_3.",".$rate_4.",".$user_id.",".$comment_id.",'".$pc_ip."','".$pros."','".$cons."')");
//$multi_in = multiRatingWidth($p_id, $rating_cat_numbers, $rate_1_find, $rate_2_find, $rate_3_find, $rate_4_find);
die();
}
add_action('wp_ajax_updateRating', 'implement_ajax_updateRating');
add_action('wp_ajax_nopriv_updateRating', 'implement_ajax_updateRating');
但警报正在返回-1&没有任何东西保存在数据库中。