从Ajax调用PHP函数

时间:2015-03-03 18:55:15

标签: javascript php jquery ajax

我是AJAX的新手,无法运行php函数。

AJAX帖子请求正常运行,这是代码:

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
    }
}

count变量是相同的。这里有什么不对吗?

1 个答案:

答案 0 :(得分:2)

您使用get参数而不是发送post参数。请参阅docs

中的示例
$.ajax({
  type: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
    alert( "Data Saved: " + msg );
});