Welcome Guest, Not a member yet? Register   Sign In
How to use POST while keeping CSRF as true
#3

This need not be tedious. Since you are using form_open() much is greatly simplified mostly because doing so adds the hidden field with the CSRF token and hash to the form. This field can easily be included in the data posted by $.ajax.

The easiest way to proceed is change your JavaScript so that Instead of getting the values of the form inputs individually use the JQuery method .serializeArray()

Here would be my preferred way to go about this.

Code:
$(document).ready(function () {
    var  baseURL = window.location.protocol + "//" + window.location.hostname;

    $("#country_actvity_search").submit(function (e) {
        e.preventDefault();
        var postData = $( this ).serializeArray();
        var url = baseURL+'/user/country_actvity_search';

        $.ajax({
            url: url,
            method: 'POST',
            data: postData,
            success: function (res)
            {
                $("#charts_country_report").html(res);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(errorThrown);
            }
        });
        
    });
});

I try to avoid executing PHP code in the JavaScript. Trying to do so almost always turns into a "gotcha" in my experience. That's why I combine these two lines to create the URL for the ajax call

Code:
var  baseURL = window.location.protocol + "//" + window.location.hostname;
//and later
var url = baseURL+'/user/country_actvity_search';
Reply


Messages In This Thread
RE: How to use POST while keeping CSRF as true - by dave friend - 12-28-2017, 02:24 PM



Theme © iAndrew 2016 - Forum software by © MyBB