Jump to content

[solved] How to develop an AJAX module


bourdux

Recommended Posts

Hello everyone,

 

I am currently developing a module that allows the user to autocomplete his/her address from the postal code. I would like tthe user to just have to click a button in the address form to autocomplete the fields by using an AJAX call made with JQuery.

 

The database side is all completed but I'm wondering how to develop an AJAX module. I've looked into the AJAX cart module but it was made for Prestashop 1.2 and I do not really understand how it works. Could anyone explain me how to develop such a module, or point me to useful resources toward my goal?

 

Thank you in advance.

Link to comment
Share on other sites

I second Vekia. Furthermore, you should use a bridge file between your class and actual script. Call this (say ajax.php) from your javascript ajax call, and pass parameters.

 

In ajax.php, embed your prestashop configuration files and call the wanted module/class method. Then simply echo out the result, or die with json_encode depending on the result your ajax call expects

Link to comment
Share on other sites

Thank you for your answers. I was speaking about the server side, not the client side, so yes, it might have been a mistake to call this question "AJAX module". I just want to make a script that will take the postcode as input, and gives the address as output.

 

In ajax.php, embed your prestashop configuration files and call the wanted module/class method. Then simply echo out the result, or die with json_encode depending on the result your ajax call expects]

 

@Nemo1: So you mean that I shouldn't write a specific module but just a simple PHP file as I would do without prestashop? So if I understand you correcly, by including the configuration files, I can still use all the methods, such as accessing the context to check if the user is logged in, as if I was programming a module.

 

Am I mistaken?

 

The thing is that I want only my users to be able to use this service, and forbid the access from outside my websites. I noticed that the Ajax Cart was using a token of some sort...

 

Finally, I was planning to be make this module to the community at some point. In that case, I just make a classic module that will just add a hook to the address form via the displayHeader, that will add the "autocomplete" button via javascript. I can't see any simpler solution since there's no hook to the address form.

Edited by bourdux (see edit history)
Link to comment
Share on other sites

Well, you can do it with a module, or without. The important thing is, yes, to include config files so that prestashop doesn't break (for security)

 

You won't be able to access the context directly, cause $this is only accessible from modules, but I believe you can access it like

 

$module = new BlockCart (just an example)

 

$module->context->controller->addCSS... etc...

Link to comment
Share on other sites

To mark a topic as [solved] :

- Edit the first post of your topic by clicking on the "Edit" button,

- Click on the "Use full editor" button,

- Add the "[solved]" string at the beginning of your topic title and click on the "Submit Modified Post" button.

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...