关于ajax参数的动态数据

时间:2016-03-25 19:55:52

标签: php jquery ajax

我使用ajax在提交时将数据发送到php代码。 现在,如何在ajax上创建参数数据是动态的另一种情况。 代码看起来像这样:

$('#print-laporan').click(function () {
    var result = $("#hasil-pencarian").clone().end().html();

    if (jenis_laporan === 'bulanan') {
        url_l = "<?= site_url('members/it/p_request/generate_pdf_laporan_bulanan') ?>";
        dataForAjax = ???

    } else if (jenis_laporan === 'mingguan') {

        url_l = "<?= site_url('members/it/p_request/generate_pdf_laporan_mingguan') ?>";
        dataForAjax = ???
    } else {
        url_l = "<?= site_url('members/it/p_request/generate_pdf_laporan_harian') ?>";
        dataForAjax = ???
    }

    $.ajax({
        url: url_l,
        type: 'POST',
        data: ??? // I want to make it dynamic based 'dataForAjax' above,
        success: function () {
            console.log(url_l);
            window.open("<?php echo base_url() . 'assets/tmp/file_laporan.pdf' ?>", "_blank", "top=25, left=250, toolbar=no, width=1000, height=600");

        },
        error: function () {
            alert('Error');
        }
    });


});

说完

if jenis_laporan == 'bulanan' {data : {firstDate : $('some_selector').val(), lastDate : $('some_selector').val(), result: result }

if jenis_laporan == 'harian' {data : {date : $('some_selector').val(), result: result }} ` 

我怎样才能成真?

1 个答案:

答案 0 :(得分:0)

我不确定这是否是您正在寻找的内容,如果情况并非如此,请进一步解释。

$('#print-laporan').click(function () {
    var result = $("#hasil-pencarian").clone().end().html();

    if (jenis_laporan === 'bulanan') {
        url_l = "<?= site_url('members/it/p_request/generate_pdf_laporan_bulanan') ?>";
        dataForAjax = {
            firstDate: $('some_selector').val(),
            lastDate : $('some_selector').val(),
            result: result
        }
    } else if (jenis_laporan === 'mingguan') {
        url_l = "<?= site_url('members/it/p_request/generate_pdf_laporan_mingguan') ?>";
        dataForAjax = {
            // Whatever you want
        }
    } else {
        url_l = "<?= site_url('members/it/p_request/generate_pdf_laporan_harian') ?>";
        dataForAjax = {
            date : $('some_selector').val(),
            result: result
        }
    }

    $.ajax({
        url: url_l,
        type: 'POST',
        data: dataForAjax,
        success: function () {
            console.log(url_l);
            window.open("<?php echo base_url() . 'assets/tmp/file_laporan.pdf' ?>", "_blank", "top=25, left=250, toolbar=no, width=1000, height=600");
        },
        error: function () {
            alert('Error');
        }
    });
});