Magento form post action to custom controller not working - wrong routing, but why?

EOB picture EOB · Feb 16, 2012 · Viewed 16.4k times · Source

I have a form in my Magento store, the contact form. When I navigate to the form, it has this URL: http://www.domain123.com/contact/. There is a form and it looks like this:

<form id="contactForm" method="post" action="contactform/index/sendcontactmail">
    ...
</form>

Now, when I click the send button, it gives me a 404 not found and I know why. The URL it posts to is http://www.domain123.com/contact/contactform/index/sendcontactmail, which is not right of course, it has to be http://www.domain123.com/contactform/index/sendcontactmail (without contact). The question is, how do I solve this issue? The contact form as the frontname contact and my modules name is contactform. How would I solve this?

Thanks!

Answer

clockworkgeek picture clockworkgeek · Feb 17, 2012

Add a leading slash to your form:

<form id="contactForm" method="post" action="/contactform/index/sendcontactmail">

Or if working from a .phtml template the safer way is to use a bit of PHP. This will insert the correct domain:

<form id="contactForm" method="post"
    action="<?php echo $this->getUrl('contactform/index/sendcontactmail') ?>">