Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call ajax in prestashop front end custom module

I have a module which creates a front end page which displays winners of particular draw,I want to add a filter by year (winning year) when i change the year it must go by ajax request and change my div

in my display.tpl for the front end i added below javascript

<script type="text/javascript">
{literal}
function QuickLook() {
    var year = $("#year").val();
    alert(year);
    $.ajax({
            url:  baseDir+'/modules/addwinners/controllers/front/displaybyajax.php',
            type: 'get',
            data: 'ajax=true&year='+year,
            success: function(response) {
                alert(response);
                console.log('success');
                // OTHER SUCCESS COMMAND - CHECK THE RETURN VALUE
                document.getElementById("winnersDiv").innerHTML=response;
            }
    });
    return false;
}

{/literal}

but its complaining Fatal error: Class 'ModuleFrontController' not found

like image 469
Jerry Abraham Avatar asked Jan 21 '26 19:01

Jerry Abraham


1 Answers

You can't access your module controller directly using its full path: /modules/addwinners/controllers/front/displaybyajax.php by accessing your controller this way, the dispatcher is not called and Prestashop Core classes are not loaded.

You need to call your controller the Prestashop way:

<script type="text/javascript">
{literal}
    function QuickLook() {
        var year = $("#year").val();
        alert(year);
        $.ajax({
            url: baseDir + 'index.php?controller=displaybyajax&redirect=module&module=addwinners',
            type: 'get',
            data: 'ajax=true&year='+year,
            success: function(response) {
                alert(response);
                console.log('success');
                // OTHER SUCCESS COMMAND - CHECK THE RETURN VALUE
                document.getElementById("winnersDiv").innerHTML=response;
            }
        });
        return false;
    }
{/literal}
</script>
like image 88
Florian Lemaitre Avatar answered Jan 25 '26 11:01

Florian Lemaitre