简体   繁体   English

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

[英]jQuery and Ajax on Form submit - parse POST data

I have the following ajax jQuery code that on document.ready function downloads a file from ajaxFileDownload.php. 我在document.ready函数上有以下ajax jQuery代码,可从ajaxFileDownload.php下载文件。

However, I would like it to instead of document.ready function use on submit of a form called reports. 但是,我希望它可以代替document.ready函数在提交称为报表的表单时使用。 So when i click submit on my form name report, then it runs this, I would also like to parse the form field post variable called user_id to the php file. 因此,当我在表单名称报告上单击“提交”时,它将运行该表单,我还想将名为user_id的表单字段发布变量解析为php文件。

Any ideas how this can be done? 任何想法如何做到这一点?

I added: $('#reports').on('submit', function(e) { 我添加了: $('#reports').on('submit', function(e) {

How can I add the user_id post variable? 如何添加user_id发布变量?

$(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!
    });
});

Try adding these two lines inside the $.fileDownload block: 尝试将这两行添加到$.fileDownload块中:

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

like so: 像这样:

$.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
        });
    }
});

That should send all form data to the PHP script. 那应该将所有表单数据发送到PHP脚本。

If you want to send just the one field, you could use $('#user_id') in place of $(this) assuming the field in question has id="user_id" . 如果您只想发送一个字段,则可以使用$('#user_id')代替$(this) ,前提是所讨论的字段具有id="user_id"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM