我是 AJAX 新手,无法运行 php 函数。
AJAX post 请求正常工作,代码如下:
function thumbs(i) {
$('.thumbs-up' + String(i)).click(function(){
$(this).addClass('up');
$.ajax({
type:"POST",
url:"item.php",
data:'act=up&function' + String(i) + '=true&user=' + email,
success: function(){
}
});
});
for (var i = 1; i <= count; i++) {
thumbs(i);
}
以上工作,我收到了所有正确的值。
那么应该运行的 PHP 代码在这里:
for ($i = 1; $i <= $count; $i++) {
if ($_POST['function' . $i] == 'true') {
//code that should run but does not
}
}
计数变量是一样的。这里有什么问题吗?
最佳答案
您正在使用 get
参数而不是发送 post
参数。请参阅 docs 中的示例
$.ajax({
type: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});
关于javascript - 从 Ajax 调用 PHP 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28839729/