Call a PHP file with AJAX in Magento

Call a PHP file with AJAX in Magento

September 1, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Call a PHP file with AJAX in Magento

Calling a PHP File with AJAX in Magento

It seems like you’re having trouble getting your AJAX call to work correctly within your Magento module. Let’s break down your issue step by step.

Understanding the Current Setup

You have set up a basic Magento module named AdiGh_askStore. The configuration file (config.xml) is correctly routed under the estores front name and has layout updates for both the default view and the specific controller actions. This ensures that your block can be rendered in the content area of the page.

The Controller Structure

Your controllers (IndexController.php and AjaxController.php) are extending Mage_Core_Controller_Front_Action, which is the base controller class for all Magento controllers. However, these controllers currently only call loadLayout() and renderLayout(), which render the layout file without any data processing or output.

The Layout XML

Your layout file (estores.xml) defines two layout handles: one for the default view (default) and one for each of your specific controller actions (askstore_index_index and askstore_ajax_index). The default handle simply references the content block. The action-specific handles add a new block with type askstore/askstore, which is rendered from the template file specified.

The Template File

The estores.phtml file contains the HTML structure for your view and the JavaScript function that should handle the AJAX call. This function, showEmail(str), sends an AJAX GET request to the ajax/index/store/q= URL with the selected store ID as a parameter when the select element’s value changes.

The Block Class

The AskStore.php block class contains three methods: getId(), getName(), and getEmail(). These methods query the database for the IDs, names, and emails of all stores using Magento’s core resource model. They then return the data as arrays, which are used to populate the select element in your view.

The AJAX Response Function

The theResponse() method is triggered by the AJAX call in your template file. It first gets the value passed from the query string (the selected store ID). If the ID is 0, it echoes a specific message. Otherwise, it queries the database for the store with the matching ID and formats the result as HTML.

The Issue

The issue lies in how the JavaScript function is interacting with the Magento URL structure and the server response. When you make an AJAX call to ajax/index/store/q=, Magento doesn’t know how to route this request because it’s not a standard controller action.

The Solution

To fix this, we need to create a proper AJAX responder in your module. Here’s what we’ll do:

  1. Create an IndexController.php file under app/code/local/AdiGh/AskStore/controllers/Ajax

    Make sure the file is named IndexController.php and is inside the Ajax subfolder of your controller folder.

  2. Define a New Action in Your Controller

    In this new controller, define an action called indexAction. This will be the target for your AJAX request.

  3. Process the Data in the Controller

    In the indexAction, you can process the data passed from the AJAX call (the selected store ID) and return the response as a JSON or XML structure that can be easily parsed by JavaScript on the client side.

  4. Update Your JavaScript Function

    Update your showEmail(str) function to make an AJAX request directly to this new controller action, using the appropriate URL structure for Magento (e.g., /estores/askstore/ajax/index).

  5. Return the Response in the Controller Action

    In your indexAction, use Mage_Core_Controller_Front_Action’s built-in methods to return a JSON or XML response with the processed data.

Remember that working with AJAX in Magento requires understanding the URL structure and how controller actions are routed. Always ensure you’re following the correct naming conventions and using the appropriate controller classes for your tasks.

I recommend checking the official Magento documentation on AJAX Communication to learn more about best practices and available methods for handling AJAX requests in a Magento application.

For further help and examples, you can visit our Magento development section at Laravel Company. We regularly publish tutorials and guides on Magento development, including working with controller actions and AJAX.

Let me know if you have any questions or need more clarification!

Extensions Themes Configuration Email

Our network