javascript - 从 Ajax 调用 PHP 函数

标签 javascript php jquery ajax

我是 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/

相关文章:

php - 数据 future 自 MySQL 的下拉框中

javascript - 创建动态 slider jQuery

javascript - 鼠标光标仅在特定元素上发生变化

javascript - 删除对象文字中的事件监听器丢失此引用

jquery - CSS 没有链接/显示?

javascript - 我可以从现有元素中获取 jQuery 对象吗

jQuery.noConflict() 破坏插件

javascript - 为什么我不能从图像 map 制作 SVG?

php - 带有@的DSN连接和密码

php - 如何使用 PHP 按标签拆分文本?