Magento: Redirecting on Form Submission
In Magento CE 1.7, redirecting upon form submission can be achieved using various methods. Let’s explore how to handle this in your specific scenario.
The Current Setup
You’ve created a CMS page with a simple “Accept Terms” checkbox form at the bottom:
<form action="url-to-go-to.html" method="GET" onsubmit="return checkCheckBox(this)">
I accept: <input type="checkbox" value="0" name="agree">
<input type="submit" value="Continue">
<input type="button" value="Exit" onclick="document.location.href='BACKTOWHAT.html';">
</form>
The JavaScript function checkCheckBox is validating the checkbox before submission:
<script type="text/javascript">
<!--
function checkCheckBox(f){
if (f.agree.checked == false )
{
alert("Please tick the box to continue");
return false;
} else
return true;
}
-->
</script>
However, you’re currently specifying the redirection URL directly in the action attribute and onclick events:
<form action="domainname.com/shop.html">
<!-- store section to go to -->
The Challenge
You want to avoid hardcoding the URLs and instead use a Magento function for redirection. You attempted using the core Mage_Core_Controller_Varien_Action::redirect() method in your CMS page:
<form action="<<?php echo $this->redirect('shop.html'); ?>" method=.... >
But this didn’t work as expected because the $this context within a CMS page doesn’t refer to a controller instance.
The Solution
Magento provides several ways to handle redirection from a CMS page. Here are two common approaches:
1. Using URL Helper Functions
Magento provides URL helper functions that can be used in any template file, including CMS pages. You can use the Mage::getUrl() function to generate URLs dynamically.
For your scenario, you would update the form action like this:
<form action="<?php echo Mage::getUrl('shop.html') ?>" method="GET" onsubmit="return checkCheckBox(this)">
<!-- Rest of the form -->
</form>
2. Using JavaScript for Redirect
If you prefer to keep the redirection logic in your JavaScript, you can use window.location.href to redirect after a successful form submission.
First, modify your form:
<form id="terms-form" action="" method="GET" onsubmit="return checkCheckBox(this)">
<!-- Rest of the form -->
</form>
Then update your JavaScript function:
<script type="text/javascript">
<!--
function checkCheckBox(f){
if (!f.agree.checked) {
alert("Please tick the box to continue");
return false;
} else {
window.location.href = '<?php echo Mage::getUrl('shop.html') ?>';
return true;
}
}
-->
</script>
In this updated version, after the checkbox validation passes, the user is redirected to the shop.html page using JavaScript.
Conclusion
When working with Magento CMS pages and form redirections:
- Use Magento’s built-in URL helper functions like
Mage::getUrl()for generating URLs. - Keep your core business logic in PHP where possible.
- Use JavaScript for client-side validations only, while keeping the main redirect logic in PHP.
Remember to clear your cache after making changes to Magento template files. This will ensure that your updates are reflected on the live site.
For more advanced redirection techniques and best practices, check out our guide on Magento URL Routing at Laravel Company.