表单上的jQuery和Ajax提交 - 解析POST数据

时间:2014-02-23 16:18:12

标签: javascript jquery ajax

我有以下ajax jQuery代码,document.ready函数从ajaxFileDownload.php下载文件。

但是,我希望在提交名为报告的表单时使用document.ready函数代替user_id。所以,当我点击我的表单名称报告上的提交,然后运行它,我还想将名为$('#reports').on('submit', function(e) {的表单字段变量解析为php文件。

任何想法如何做到这一点?

我添加了:$(function () { $('#reports').on('submit', function(e) { var $preparingFileModal = $("#preparing-file-modal"); $preparingFileModal.dialog({ modal: true }); $.fileDownload('ajaxFileDownloader.php?' + Math.random(), { successCallback: function (url) { $preparingFileModal.dialog('close'); }, failCallback: function (responseHtml, url) { $preparingFileModal.dialog('close'); $("#error-modal").dialog({ modal: true }); } }); return false; //this is critical to stop the click event which will trigger a normal file download! }); });

如何添加user_id post变量?

{{1}}

1 个答案:

答案 0 :(得分:0)

尝试在$.fileDownload块中添加这两行:

httpMethod: 'POST',
data: $(this).serialize()
像这样:

$.fileDownload('ajaxFileDownloader.php?' + Math.random(), {
    httpMethod: 'POST',
    data: $(this).serialize(),
    successCallback: function (url) {
        $preparingFileModal.dialog('close');
    },
    failCallback: function (responseHtml, url) {
        $preparingFileModal.dialog('close');
        $("#error-modal").dialog({
            modal: true
        });
    }
});

这应该将所有表单数据发送到PHP脚本。

如果您只想发送一个字段,则可以使用$('#user_id')代替$(this),假设相关字段为id="user_id"

相关问题