Magento: Sending Data from Controller to PHTML File via AJAX/JSON
Introduction
In this post, we’ll guide you through sending data from your controller to a PHTML file using AJAX and JSON in Magento. Specifically, we’ll demonstrate how to update the content of an HTML element on your PHTML page as the controller processes a loop.
The Controller Setup
In your controller’s action method (let’s call it runremitAction), you likely have a loop that performs some operations on each iteration:
foreach($varlines as $vars => $variable)
{
// Some processing code here
}
// Rest of the code
The goal is to update an element on your PHTML page with the current order number being processed in this loop.
Sending Data via AJAX
To accomplish this, we’ll use a combination of JSON and JavaScript (along with jQuery for simplicity). Here’s how you can modify your controller:
Step 1: Prepare the data
First, prepare an array containing the data you want to send. In your case, it seems like you want to send the order number, amount, and check number.
$result = array(
"Order" => $ordernum,
"Amount" => $amount,
"CHQ Num" => $chqnumber
);
Step 2: Set the response
Next, set your controller’s response to send this data as a JSON string:
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
This will automatically convert the $result array into a JSON string and send it back in the response.
Step 3: JavaScript in PHTML
Now, let’s modify your PHTML file to handle this data using AJAX.
The PHTML Setup
Step 1: Add an element to display the data
First, add a textarea or any other HTML element where you want to display the order number:
<textarea id="order-data" rows="4" cols="50"></textarea>
Step 2: Add JavaScript code
Next, add some JavaScript (or jQuery) code that will listen for a response from your controller and update the textarea:
<script type="text/javascript">
var editForm = new varienForm('edit_form');
// Listen for the form submission event
editForm.submit(function() {
$.ajax({
url: "<?php echo $this->getUrl('*/*/runremit') ?>",
dataType: 'json',
success: function(data) {
// Update the textarea with the received order number
$('#order-data').val(JSON.stringify(data));
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus);
}
});
});
</script>
This code will:
- Listen for the form submission event (when you click the “Submit & Run” button).
- Send an AJAX request to the
runremitActionURL. - Receive the JSON response from the controller.
- Update the value of the
textareawith the received data.
Conclusion
In this post, we demonstrated how to send data from a Magento controller to a PHTML file using AJAX and JSON. By updating your controller to prepare and send the desired data as a JSON string in the response, and adding some JavaScript code to listen for and handle that response in your PHTML file, you can achieve real-time updates on the page while processing loop iterations.
Remember to test this setup thoroughly in a development environment before deploying it to a live site. If you’re looking for professional Magento services or need further assistance with your e-commerce development, feel free to reach out to Laravel Company.