Question

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.

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top