Jump to content

How to use ajax to pass data to a ModuleAdminController?


Recommended Posts

Hello, I have made an admin controller for my module and I wish to send some data to it using ajax. Is this possible?

 

If it is possible, how can I do that?

 

Help would be greatly appreciated.

Link to comment
Share on other sites

Just add a function starting with process to your controller. For example:
    public function processRegenerate()
    {
        echo 'Regenerating...';
        die;
    }

Then simply add &action=regenerate to the end of your controller's URL to call the function. Change regenerate to any string that describes what your function is doing.

  • Like 1
Link to comment
Share on other sites

Hello and thanks for the reply Rocky, I did not know that. Is there a similar way to send a value for a variable and access it using $_GET method in the controller and then display it in the view?

Link to comment
Share on other sites

You can add more query strings to the URL and then access the data within the function. For example, if the query string is &action=regenerate&type=images:
public function processRegenerate()
{
    $type = Tools::getValue('type');
    echo $type;
​    die;
}

This will save "images" to the $type variable and then print it out. You can then read it from result function of the AJAX call.

  • Like 2
Link to comment
Share on other sites

  • 3 years later...

Good day - 3 years later and I can not for the life of me get an ajax call to work.

PrestaShop: 1.7.6.5
Module: CustomModule

Ajax URL:
https://domain/module/CustomModule/CustomModuleOrderManagerController?action=productStockIssue&id_product=###&id_order=###&ajax=true

class CustomModuleOrderManagerController extends ModuleAdminController {
	public function processProductStockIssue() {
		$this->ajaxDie( Tools::jsonEncode( ['error' => 'This is a test 2.'] ) );
	}
}

Am I missing something? It just responds with 404 not found with everything that I try.

Edited by Estian
Incomplete (see edit history)
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...