Search code examples
phpmagentomagento-1.9

Form sometimes submitting data, sometimes not. Magento 1.9


I have a page where I call a form, receiving reports from users that sometimes it just doesn't submit properly. Not sure why. Any ideas?

Users have been reporting this in various browsers, so it's been especially difficult to track down. Chrome, IE, Edge, Safari, etc. have all reported it, but sometimes it just works.

What the form is supposed to do is take the entered data, then email it to a specific email address.

Magento Block:

<div class="page-title">
   <h1>Program</h1>
</div>
<img class="textwrap-right" src="{{media url="wysiwyg/cfi_rebate_badge.png"}}" alt="" />
<div class="std">
   <h3 class="ui-helper-clearfix">Sign up below.</h3>
   {{widget type="mailform/widget_form" form_template="contacts/cfi.phtml"}}<br />
</div>

This is my contacts/cfi.phtml template:

<?php
$directory = Mage::helper('directory');
$customer = Mage::getSingleton('customer/session')->getCustomer();
$customerAddress = $customer->getAddressById( $customer->getDefaultBilling() );
if($customerAddress->getCountryId() != 'US'){
    $customerAddress = Mage::getModel('customer/address');
}
?>
<div id="messages_product_view"><?php echo $this->getMessagesBlock()->getGroupedHtml() ?></div>

<form action="<?php echo $this->getFormAction() ?>" id="contactForm" method="post" class="scaffold-form">
    <div class="fieldset">
        <h2 class="legend"><?php echo Mage::helper('contacts')->__('Contact Information') ?></h2>
        <p class="required"><?php echo Mage::helper('contacts')->__('* Required Fields') ?></p>
        <ul class="form-list">
            <li class="fields">
                <div class="field">
                    <label for="cfirebate:name" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Name') ?></label>
                    <div class="input-box">
                        <input name="cfirebate[name]" id="cfirebate:name" title="<?php echo Mage::helper('contacts')->__('Name') ?>" value="<?php echo $this->escapeHtml($this->helper('contacts')->getUserName()) ?>" class="input-text required-entry" type="text" />
                    </div>
                </div>
                <input type="hidden" name="cfirebate[country]" id="cfirebate:country_id" value="US">
                <div class="field">
                    <label for="cfirebate:address" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Address') ?></label>
                    <div class="input-box">
                        <input name="cfirebate[address]" id="cfirebate:address" title="<?php echo Mage::helper('contacts')->__('Address') ?>" class="input-text required-entry" type="text" />
                    </div>
                </div>
                <div class="field">
                    <label for="cfirebate:city" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('City') ?></label>
                    <div class="input-box">
                        <input name="cfirebate[city]" id="cfirebate:city" title="<?php echo Mage::helper('contacts')->__('City') ?>" class="input-text required-entry" type="text" />
                    </div>
                </div>
                <div class="field">
                    <div class="field">
                        <label for="cfirebate:region" class="required"><em>*</em><?php echo $this->__('State/Province') ?></label>
                        <div class="input-box">
                            <select id="cfirebate:region_id" name="cfirebate[region_id]" title="<?php echo $this->__('State/Province') ?>" class="validate-select" style="display:none;">
                                <option value="">Please select region, state or province</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="field">
                    <label for="cfirebate:postcode" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Zip') ?></label>
                    <div class="input-box">
                        <input name="cfirebate[postcode]" id="cfirebate:postcode" title="<?php echo Mage::helper('contacts')->__('Zip') ?>" class="input-text required-entry" type="text" />
                    </div>
                </div>
                <div class="field">
                    <label for="cfirebate:email" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Email') ?></label>
                    <div class="input-box">
                        <input name="cfirebate[email]" id="cfirebate:email" title="<?php echo Mage::helper('contacts')->__('Email') ?>" class="input-text required-entry validate-email" type="email" autocapitalize="off" autocorrect="off" spellcheck="false" />
                    </div>
                </div>
            </li>
            <li>
                <label for="cfirebate:telephone"><?php echo Mage::helper('contacts')->__('Phone Number') ?></label>
                <div class="input-box">
                    <input name="cfirebate[telephone]" id="cfirebate:telephone" title="<?php echo Mage::helper('contacts')->__('Phone Number') ?>" class="input-text" type="tel" />
                </div>
            </li>
            <li class="wide">
                <label for="cfirebate:comment"><?php echo Mage::helper('contacts')->__('Comment') ?></label>
                <div class="input-box">
                    <textarea name="cfirebate[comment]" id="cfirebate:comment" title="<?php echo Mage::helper('contacts')->__('Comment') ?>" class="input-text" cols="5" rows="3"></textarea>
                </div>
            </li>
        </ul>
    </div>
    <div class="buttons-set">
        <input type="text" name="hideit" id="hideit" value="" style="display:none !important;" />
        <button type="submit" title="<?php echo Mage::helper('contacts')->__('Submit') ?>" class="button"><span><span><?php echo Mage::helper('contacts')->__('Submit') ?></span></span></button>
    </div>
</form>
<script type="text/javascript">
//<![CDATA[
    var contactForm = new VarienForm('contactForm', true);
    var cfiRegionUpdater = new RegionUpdater('cfirebate:country_id', 'cfirebate:region', 'cfirebate:region_id', <?php echo $directory->getRegionJson() ?>, undefined, 'cfirebate:postcode');
//]]>
</script>

Solution

  • Try adding form key inside the form:

    <input type="hidden" name="form_key" value="<?php echo $this->getFormKey(); ?>">
    

    Just add the above code anywhere inside your form markup.