简体   繁体   中英

load php page from jquery ajax post unloading the current page

I need to generate a report showing in a php page which will be called by a jquery ajax call. Can any body help me on how to do this.

The jquery ajax post is as following:

$('#report_condition #Submit_rpt_betn_dates').on('click', function(){
        var start_dt = $('#report_condition').find('.rpt_betn_dates').find('.start_search_date').val();
        var end_dt = $('#report_condition').find('.rpt_betn_dates').find('.end_search_date').val();
        alert('you want to generate report between '+start_dt+' and '+end_dt);

    $.ajax({  
        type: "POST",  
        url: "./report-betn-dates.php",
        data: {'startdt':start_dt, 'enddt':end_dt}, //the first parameter in the pair is actually the key for $_POST in PHP
        //and it must be withing quotes for ajax to run!!
        crossdomain: true,
        success: function(response) {
        }
    });
});

I tried with $('body').html(response); within success parameter of the ajax call. But by this I cannot access the separate css file for the php page. Hence I would like to unload the page containing the ajax call and load the php page with the data sent through $_POST[] .

Ajax is for partial content load, asyncronous interactions , etc.. Since what you're trying to achieve is complete new page loaded with its own static resources (css, js) this is the perfect "syncronous" scenario. So, why wouldn't you use a simpler setup like a form which sends the required vars in post and moves the user to the correct php page?

$('#report_condition #Submit_rpt_betn_dates').on('click', function(){

        var fake_form   = $('<form method="post" action="report-betn-dates.php">');
        var input_start = $('<input type="hidden" name="startdt">');
        var input_end   = $('<input type="hidden" name="enddt">');

        $(fake_form).append(input_start);
        $(fake_form).append(input_end):

        $(fake_form).submit();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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