【发布时间】:2015-04-12 01:22:54
【问题描述】:
好吧,伙计们希望你们也能帮助我解决这个问题。我对 jQuery 和 AJAX 的知识并不疯狂,可能我错过了一些非常简单的东西。我无法传递超过 1 个变量。我已经尝试了几种不同的方法,其中最接近我工作的是this,但即使这样也没有用。
jQuery
$("#bodymes ul li span[contenteditable=true]").blur(function(){
var weight_id = $(this).attr("id") ;
var weightbody_mes = $(this).text() ;
$.post( "../includes/bodymes_weight.php", { weightbodymes: weightbody_mes })
.done(function( data ) {
$(".weightsuccess").slideDown(200);
$(".weightsuccess").html("Weight Updated successfully");
if ($('.weightsuccess').length > 0) {
window.setTimeout(function(){
$('.weightsuccess').slideUp(200);
}, 4000);
}
// alert( "Data Loaded: " + data);
});
});
所以基本上如果我运行它,它会工作得很好,我的 PHP 脚本会处理它。请注意,当我去提醒我并显示加载的数据时,它会显示正确的信息(来自weightbodymes 的信息)并且我能够更新 db.但是,一旦我添加另一个变量{ weightbodymes: weightbody_mes, weightid: weight_id },它就不会显示在警报框中加载的数据(如果我尝试显示来自这两个变量的信息它正在工作,但它只向 PHP 提交一个变量是:
$weightid = $_POST['weightid'];
$weight = $_POST['weightbodymes'];
if ($insert_stmt = $mysqli->prepare("UPDATE body SET body_weight=? WHERE body_id='$weightid'")) {
$insert_stmt->bind_param('s', $weight);
// Execute the prepared query.
if (! $insert_stmt->execute()) {
header('Location: ../index.php?error=Registration failure: INSERT nwprogram1');
}
}
希望你能告诉我我在哪里犯了错误以及如何纠正它。 提前谢谢您!
【问题讨论】:
-
向
data对象添加更多属性:{ weightbodymes: weightbody_mes, weightid: weight_id, foo: 'bar' } -
发送类似
{'data':{'var_1': 'value1', 'var_2' : 'value2'}}的内容。然后通过$_POST['data']['var_1'], $_POST['data']['var_2']访问它 -
我尝试了第一个和第三个答案,但似乎不起作用。生病再试几次。谢谢!
标签: javascript php jquery mysql ajax