0

I have different forms in my project. i want to when user submit request then page id will automatically save in database.

this is my contact us form form.phtml

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

<div class="row box-1-contact">
<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(); ?>contactus/index/updateinfo" 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-envelope" aria-hidden="true"></i>
</span>
                        <input name="email" id="contactEmail" title="<?php echo Mage::helper('contacts') ?>" onfocus="if(this.value=='<?php echo Mage::helper('contacts') ?>') this.value='';" onblur="if(this.value=='') this.value='<?php echo Mage::helper('contacts') ?>';" value="<?php echo ($this->helper('contacts')->getUserEmail())?$this->htmlEscape($this->helper('contacts')->getUserEmail()):Mage::helper('contacts'); ?>" class="form-control required-entry validate-email" type="text" placeholder="Email"/>

                    </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="contactNumber" title="<?php echo Mage::helper('contacts')->__('Title') ?>" onfocus="if(this.value=='<?php echo Mage::helper('contacts') ?>') this.value='';" onblur="if(this.value=='') this.value='<?php echo Mage::helper('contacts')?>';" value="<?php echo ($this->helper('contacts')->getUserEmail())?$this->htmlEscape($this->helper('contacts')->getUserEmail()):Mage::helper('contacts'); ?>" class="form-control" type="text" placeholder="Contact Number"/> 

                </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="contactComment" title="<?php echo Mage::helper('contacts')->__('Comment') ?>" onfocus="if(this.value=='<?php echo Mage::helper('contacts')?>') this.value='';" onblur="if(this.value=='') this.value='<?php echo Mage::helper('contacts')?>';" value="<?php echo Mage::helper('contacts'); ?>" class="required-entry form-control" cols="10" rows="7" placeholder="Comment"><?php echo Mage::helper('contacts') ?></textarea>
                </div>
                </div></br>
            </li>

        </ul>
    </div>
    <div class="buttons-set">
        <input type="text" name="hideit" id="hideit" value="" style="display:none !important;" />
        <button id="contactBtn" 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>
<div id="formSuccess" style="display:none;">&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 = 'contactForm'; 
    var postUrl = '<?php echo $this->getUrl("contactus/index/updateinfo") ?>';
    var email = $('contactEmail').getValue();
    var number = $('contactNumber').getValue();
    var comment = $('contactComment').getValue();
    var ip_address = '<?php echo $_SERVER["REMOTE_ADDR"] ?>';
    var errorFlag = false;


    if (!errorFlag) {
        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, inquiry_source : 'Contact_Us' , ip_address : ip_address, comment : comment},
            }
        );
    }
    }    
new Event.observe('contactBtn', 'click', function(e){
    e.stop();
    doAjax();
    });

//]]>
</script>

and this is my controller for saving this form in database Indexcontroller.php

<?php
class Pfy_Contactus_IndexController extends Mage_Core_Controller_Front_Action{

public function IndexAction() {

  $this->loadLayout();   
  $this->getLayout()->getBlock("head")->setTitle($this->__("Contactus"));
        $breadcrumbs = $this->getLayout()->getBlock("breadcrumbs");
  $breadcrumbs->addCrumb("home", array(
            "label" => $this->__("Home Page"),
            "title" => $this->__("Home Page"),
            "link"  => Mage::getBaseUrl()
       ));

  $breadcrumbs->addCrumb("contactus", array(
            "label" => $this->__("Contactus"),
            "title" => $this->__("Contactus")
       ));

  $this->renderLayout(); 

    }

    public function UpdateinfoAction() {

    $email = $this->getRequest()->getParam('email');
    $number = $this->getRequest()->getParam('number');  
    $comment = $this->getRequest()->getParam('comment');
    $inquiry_source = $this->getRequest()->getParam('inquiry_source');  
    $ip_address = $this->getRequest()->getParam('ip_address');

    $data = array(
        'email' => $email,  
        'number' => $number,            
        'comment' => $comment,
        'inquiry_source' => $inquiry_source,
        'ip_address' => $ip_address

    );
    $model = Mage::getModel('contactus/contactus'); //for eg.     Mage::getModel('catalog/product');
    try {
        $model->setData($data)
            ->save();

             $jsonData = json_encode(array('error' => false));
            $this->getResponse()->setHeader('Content-type', 'application/json');
            $this->getResponse()->setBody($jsonData);               

}


}

after submit this form i want to save current page id in database automatically.

  • What is 'current page id'? – Sohel Rana Jan 4 '17 at 7:35
  • i dont ever define page id to any page but i want to save this page id in database after form submit. so what i have to do for this? – Neeya Jan 4 '17 at 7:42
  • What do you mean 'current page id'? It's current page URL? – Sohel Rana Jan 4 '17 at 7:53
  • yes it's current page url but i have to save only page id in database – Neeya Jan 4 '17 at 7:56
  • generally every url having page id so i need save only this id in database – Neeya Jan 4 '17 at 7:58
0

If you have added your form on cms pages and want to save the id, I will recommend to create a field for this in your table, e.g "page_id varchar(150)" . Add this code inside your form :

<?php 

    $cmsId = Mage::helper('core/url')->getCurrentUrl();

?>
<input type="hidden" value="<?php echo $cmsId; ?>" name="page_id" />

And in your controller add this code:

$cmsId = $this->getRequest()->getParam('page_id');

 $data = array(
        'email' => $email,  
        'number' => $number,            
        'comment' => $comment,
        'inquiry_source' => $inquiry_source,
        'ip_address' => $ip_address,
        'page_id' => $cmsId,

    );

Hope this will help you.

  • but how to define page_id it is hidden field. so what is the value of this page_id field – Neeya Jan 4 '17 at 9:41
  • i tried this but database caching zero value – Neeya Jan 4 '17 at 9:43
  • @Kiyara no need to define it in form you. this code is doing this "$cmsId = $cmsModel->getPageId();" – Arunendra Jan 4 '17 at 9:44
  • please add var_dump($cmsId) ; die; after this line "$cmsId = $cmsModel->getPageId();" and see if value is showing there – Arunendra Jan 4 '17 at 9:47
  • it showing NULL value – Neeya Jan 4 '17 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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