Jump to content

How to implement AJAX calls


Recommended Posts

I am a newbie to PrestaShop. I read the documentation but there are many questions left.

 

I created a module which shows a user form. I want to add an AJAX call.

 

When the user starts typing a name, the name will be looked up in the database and matches will be returned as JSON, The user form then shows possible matches, so the user can choose a matched one or continue typing.

 

My Frontcontroller only shows a TPL file and in the TPL file I added the AJAX code. However I am not sure if the AJAX call should call the same frontcontroller and if so, how this frontcontroller should be able to get the entered data or return the database data. Or maybe this call should go to a different class that would handle the search and data-exchange.

 

Please help.

Anneke

Link to comment
Share on other sites

What I usually do is add a line like the following to the getContent() function:

        if (Tools::getValue('action') == 'updateBlocksPosition' && Tools::getValue('ajax') == 1) {
            $this->action = 'updateBlocksPosition';
        }

and then create a function like the following:

    public function ajaxProcessUpdateBlocksPosition()
    {
        if (Tools::getValue('blocks')) {
            $blocks = Tools::getValue('blocks');

            // Perform query and encode results as JSON here
        }
    }

Change updateBlockPositions to your AJAX call's action.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...