【问题标题】:PHP and Ajax post onclick eventsPHP 和 Ajax 发布 onclick 事件
【发布时间】:2012-09-20 16:55:44
【问题描述】:

我有一个这样的脚本:

<script type="text/javascript">
function DeletePublisher(publisherid) {
jConfirm('Are you sure you want to delete this publisher?', 'Delete publisher', function(r) { if (r)

scriptitem = document.createElement('script');
scriptitem.type = 'text/javascript';
scriptitem.src = 'includes/publishers/delete-publisher.php?publisherid=' + publisherid;
scriptitem.id = 'ajax';
document.body.appendChild(scriptitem);
setTimeout('document.body.removeChild(document.getElementById("ajax"))', 500);  

$.jGrowl('Publisher deleted');
window.location.reload();
});
}
</script>

我在表格中列出这样的行:

TD ROWS HERE...
<td class="unpaid-th"><strong><?php echo $publisher_unpaid; ?></strong></td>
                <td class="action-th">
                    <ul class="button-table-head">
                        <li><div class="button-head edit-icon"><a href="#" class="sweet-tooltip" data-text-tooltip="Edit" data-style-tooltip="tooltip-mini-slick"><span>Edit</span></a></div></li>
                        <li><div class="button-head delete-icon"><a href="#" class="sweet-tooltip" data-text-tooltip="Delete" data-style-tooltip="tooltip-mini-slick" onclick="DeletePublisher('<?php echo $publisher_id; ?>')"><span>Delete</span></a></div></li>
                    </ul>
                </td>

现在应该发生的是当我单击删除链接时 - 它应该发布/获取(无论如何 - 因为我在侦听器脚本上使用 $_REQUEST)到 php 脚本,该脚本将获取发布者的 id 并将其从数据库中删除... .但问题似乎是实际上没有发送ID来删除脚本,我已经尝试了一切......

它在源代码中显示不错,例如 onclick="DeletePublisher('152')" 并提示警报、信息等...但似乎它没有发送 ID ..... 或者现在甚至可能没有调用监听器脚本(不知道如何测试):(

任何想法这里有什么问题(或者可能是不同的方法?)?

非常感谢!

【问题讨论】:

    标签: jquery ajax call


    【解决方案1】:

    我会推荐使用像下面这样使用 jQuery 的 AJAX

    function DeletePublisher(publisherid) {
      jConfirm('Are you sure you want to delete this publisher?', 'Delete publisher', function(r) { if (r)
        $.ajax({
          type: "POST", //or GET
          url: 'includes/publishers/delete-publisher.php?publisherid=' + publisherid,
          data: '',
          success: function(response){
            $.jGrowl('Publisher deleted');
            window.location.reload();
          }
        });
      });
    }
    

    【讨论】:

      【解决方案2】:

      我认为您正在尝试做的是 JSONP 调用。我认为缺少的是响应应该包含在 javascript 调用中。方法名应与回调方法名相同。

      但我认为您无需继续使用 JSNOP 方法,因为我看到您的脚本位于同一个域中。所以试着做一个简单的ajax调用。你可以试试jquery。它将照顾浏览器的兼容性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-11
        • 2016-10-10
        • 2014-09-19
        • 2011-11-17
        相关资源
        最近更新 更多