1

i need to save contact us form fields in magento database please help.

Thanks in advance.

following is the form.phtml file. file path is ,

app\design\frontend\mypackage\mytheme\template\contacts\form.phtml

<div id="messages_product_view"><?php echo $this->getMessagesBlock()->getGroupedHtml() ?></div>

<div class="row box-1-contact" id="inquiryForm">
<div class="col-md-12 page-title title-c">
    <h1><?php echo $this->__('Contact Us');?></h1>
</div>
<?php if($this->getLayout()->createBlock('cms/block')->setBlockId('google-map')->toHtml()) { ?>
<div class="contact-map-wrap col-lg-6 col-md-6 col-sm-6 col-xs-12">
    <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('google-map')->toHtml() ?>
</div>
<?php } ?>
<form class="contact-form col-lg-6 col-md-6 col-sm-6 col-xs-12" action="<?php echo Mage::getUrl(); ?>contacts/index/post/" id="contactForm" method="post">
    <div class="form-wrapper">
        <ul class="form-list">
            <li class="fields">
                <div class="field">
                    <div class="input-box">
                    <div class="input-group">
 <span class="input-group-addon" id="basic-addon1"><i class="icon-user" aria-hidden="true"></i>
</span>
     <input name="name" id="name" title="<?php echo Mage::helper('contacts')->__('Your name') ?>" onfocus="if(this.value=='<?php echo Mage::helper('contacts')->__('Your name...') ?>') this.value='';" onblur="if(this.value=='') this.value='<?php echo Mage::helper('contacts')->__('Your name...') ?>';" value="<?php echo ($this->helper('contacts')->getUserName())?$this->htmlEscape($this->helper('contacts')->getUserName()):Mage::helper('contacts')->__('Your name...'); ?>" class="form-control required-entry" type="text" />
                    </div>
                    </div>
                </div></br>
                <div class="field">
                    <div class="input-box">
                    <div class="input-group">
<span class="input-group-addon" id="basic-addon1"><i class="icon-envelope" aria-hidden="true"></i>
</span>
                        <input name="email" id="email1" title="<?php echo Mage::helper('contacts')->__('Your email') ?>" onfocus="if(this.value=='<?php echo Mage::helper('contacts')->__('Your email...') ?>') this.value='';" onblur="if(this.value=='') this.value='<?php echo Mage::helper('contacts')->__('Your email...') ?>';" value="<?php echo ($this->helper('contacts')->getUserEmail())?$this->htmlEscape($this->helper('contacts')->getUserEmail()):Mage::helper('contacts')->__('Your email...'); ?>" class="form-control required-entry validate-email" type="text" />
                    </div>
                </div>
                </div></br>
            </li>
            <li>
                <div class="input-box field">
                <div class="input-group">
            <span class="input-group-addon" id="basic-addon1"><i class="icon-pencil" aria-hidden="true"></i>
            </span>
                    <input name="subject" id="subject" title="<?php echo Mage::helper('contacts')->__('Title') ?>" onfocus="if(this.value=='<?php echo Mage::helper('contacts')->__('Subject') ?>') this.value='';" onblur="if(this.value=='') this.value='<?php echo Mage::helper('contacts')->__('Subject') ?>';" value="<?php echo ($this->helper('contacts')->getUserEmail())?$this->htmlEscape($this->helper('contacts')->getUserEmail()):Mage::helper('contacts')->__('Subject'); ?>" class="form-control" type="text" />  
                </div>
                </div></br>
            </li>
            <li class="wide">
                <div class="input-box">
                <div class="input-group">
<span class="input-group-addon" id="basic-addon1"><i class="icon-comments" aria-hidden="true"></i>
</span>
                    <textarea name="comment" id="comment" title="<?php echo Mage::helper('contacts')->__('Comment') ?>" onfocus="if(this.value=='<?php echo Mage::helper('contacts')->__('Comment...') ?>') this.value='';" onblur="if(this.value=='') this.value='<?php echo Mage::helper('contacts')->__('Comment...') ?>';" value="<?php echo Mage::helper('contacts')->__('Comment...'); ?>" class="required-entry form-control" cols="10" rows="7"><?php echo Mage::helper('contacts')->__('Comment...') ?></textarea>
                </div>
                </div></br>
            </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')->__('Send Email') ?>" class="button"><span <span><?php echo Mage::helper('contacts')->__('Send Email') ?></span></span> </button>
    </div>
</form>
<div style="clear:both; padding:15px 0px;">&nbsp;</div>

    <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('info-store')->toHtml() ?>

</div>


<script type="text/javascript">
//<![CDATA[
var contactForm = new VarienForm('contactForm', false);
function doAjax() {
    var formId = 'inquiryForm';
    var myForm = new VarienForm(formId, true);
    var postUrl = '<?php echo $this->getUrl("contactus/index/updateinfo") ?>';
    var email = $('inquiryEmail').getValue();
    var number = $('inquiryNumber').getValue();
    var comment = '<?php echo 'Inquiry request for '. $_product->getName() .' ['. $_product->getId() .']'; ?>';
   // if (myForm.validator.validate()) {
        new Ajax.Updater(
            { success:'formSuccess' }, postUrl, {
                method:'post',
                asynchronous:true,
                evalScripts:false,
                onComplete:function(request, json) {
                    var data = request.responseText.evalJSON();                     
                    if (false == data.error) {              
                        $(formId).hide();   
                        $('formSuccess').show();                            
                        $("formSuccess").update("Your request has been submitted succesfully.");                            
                    } else {
                        $("formSuccess").update("Some thing is wrong, Please try later");
                    }
                },
                onLoading:function(request, json){
                    Element.show('formLoader');
                },
                parameters: {email:email, number : number, comment : comment},
            }
        );
   // }
}  
new Event.observe('inquiryBtn', 'click', function(e){
    e.stop();
    doAjax();
}); 
//]]>
</script>

and core controller of this form is ,

app\code\core\Mage\Contacts\controllers\IndexController.php

class Mage_Contacts_IndexController extends Mage_Core_Controller_Front_Action
{
const XML_PATH_EMAIL_RECIPIENT  = 'contacts/email/recipient_email';
const XML_PATH_EMAIL_SENDER     = 'contacts/email/sender_email_identity';
const XML_PATH_EMAIL_TEMPLATE   = 'contacts/email/email_template';
const XML_PATH_ENABLED          = 'contacts/contacts/enabled';

public function preDispatch()
{
    parent::preDispatch();

    if( !Mage::getStoreConfigFlag(self::XML_PATH_ENABLED) ) {
        $this->norouteAction();
    }
}

public function indexAction()
{
    $this->loadLayout();
    $this->getLayout()->getBlock('contactForm')
        ->setFormAction( Mage::getUrl('*/*/post', array('_secure' => $this->getRequest()->isSecure())) );

    $this->_initLayoutMessages('customer/session');
    $this->_initLayoutMessages('catalog/session');
    $this->renderLayout();
}

public function postAction()
{
    $post = $this->getRequest()->getPost();
    if ( $post ) {
        $translate = Mage::getSingleton('core/translate');
        /* @var $translate Mage_Core_Model_Translate */
        $translate->setTranslateInline(false);
        try {
            $postObject = new Varien_Object();
            $postObject->setData($post);

            $error = false;

            if (!Zend_Validate::is(trim($post['name']) , 'NotEmpty')) {
                $error = true;
            }

            if (!Zend_Validate::is(trim($post['comment']) , 'NotEmpty')) {
                $error = true;
            }

            if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
                $error = true;
            }

            if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
                $error = true;
            }

            if ($error) {
                throw new Exception();
            }
            $mailTemplate = Mage::getModel('core/email_template');
            /* @var $mailTemplate Mage_Core_Model_Email_Template */
            $mailTemplate->setDesignConfig(array('area' => 'frontend'))
                ->setReplyTo($post['email'])
                ->sendTransactional(
                    Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
                    Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
                    Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
                    null,
                    array('data' => $postObject)
                );

            if (!$mailTemplate->getSentSuccess()) {
                throw new Exception();
            }

            $translate->setTranslateInline(true);

            Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
            $this->_redirect('*/*/');

            return;
        } catch (Exception $e) {
            $translate->setTranslateInline(true);

            Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
            $this->_redirect('*/*/');
            return;
        }

    } else {
        $this->_redirect('*/*/');
    }
}

}

I want to save this fields in custom table in magento database please help.

closed as too broad by Rajeev K Tomy, Marius Nov 25 '16 at 7:16

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Hi Kiyara, Welcome to Magento Stack Exchange. Please provide valid codes or be specific in your question. Your question seems to be too broad. – Rajeev K Tomy Nov 25 '16 at 6:39
  • 1
    please see updated question – Neeya Nov 25 '16 at 6:51
  • if you have any idea please share – Neeya Nov 25 '16 at 6:55
1

you need to create controller file and custom table at database to save contact us data.

review this link for more detail.

THanks

  • i tried this but not working – Neeya Nov 25 '16 at 7:06
  • <?php require_once 'Mage/Contacts/controllers/IndexController.php'; class Letsknowit_Contactus_IndexController extends Mage_Contacts_IndexController { public function postAction() { $post = $this->getRequest()->getPost(); /* code to save query , you can write our insert query here*/ if ( $post ) { $model = Mage::getModel('contactus/contactus'); $model->setData($post); $model->save(); } } ?> – Neeya Nov 25 '16 at 7:07
  • i think something goes wrong in this controller please provode me mysql query to save data – Neeya Nov 25 '16 at 7:09
  • this is my insert query – Neeya Nov 25 '16 at 7:54
  • $write = Mage::getSingleton("core/resource")->getConnection("core_write"); // Concatenated with . for readability $query = "insert into mage_example " . "(name, email, subject, comment) values " . "(:name, :email, :subject, :comment)"; $binds = array( 'name' => "name' or 1=1", 'email' => "email", 'subject' => "subject", 'comment' => "comment", ); $write->query($query, $binds); – Neeya Nov 25 '16 at 7:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.