Magento – How to save edited data in magento backend custom form

magento-1.7

I made custom menu,grid, and when i click on any row from grid it will open custom form which is also customized form at admin side. and that form filled with data which is reated to particular clicked row. now when i click on save button whatever changes i did in that form it would save in database.

How Can i save?

app\code\local\ Xyz\Example\controllers\Adminhtml\ExampleController.php

<?php
class Xyz_Example_Adminhtml_ExampleController extends Mage_Adminhtml_Controller_Action
{
public function indexAction()
{
  //  $this->_title($this->__('Example'))->_title($this->__('Example Xyz'));

    $this->loadLayout();
    $this->_setActiveMenu('example/example');
    $this->_addContent($this->getLayout()->createBlock('example/adminhtml_data_detail'));
    $this->renderLayout(); 
}

public function newAction()
{
    $this->loadLayout();
    $this->_setActiveMenu('example/example');
    $this->_addContent($this->getLayout()->createBlock('example/adminhtml_example_edit'))
         ->_addLeft($this->getLayout()->createBlock('example/adminhtml_example_edit_tabs'));
    $this->renderLayout();

}
public function editAction()
{
    $this->loadLayout();
    $this->_setActiveMenu('example/example');
    $this->_addContent($this->getLayout()->createBlock('example/adminhtml_example_edit'))
                    ->_addLeft($this->getLayout()->createBlock('example/adminhtml_example_edit_tabs'));
    $this->renderLayout();

}
 public function postAction(){

    $this->loadLayout();
    $post = $this->getRequest()->getParams();
    $model = Mage::getModel('example/example');
    $ex=$model->getData('email');

    $da =$post['id'];
    print_R($ex);
    $model->setData($da);
    $model->save();
    //print_R($post);

    //print_R($da);exit;

    //$post->setData();
//  $post->save();
    Mage::getSingleton('core/session')->addSuccess('Successfully save data...'); 
    $this->renderLayout();

} 

public function saveAction() {

    if ($data = $this->getRequest()->getPost()) {
            }
        //  $this->renderLayout();
}
public function gridAction()
{
    $this->loadLayout();
    $this->getResponse()->setBody(
        $this->getLayout()->createBlock('xyz_example/adminhtml_data_detail_grid')->toHtml()
    );
}
public function exportXyzCsvAction()
{
    $fileName = 'example_xyz.csv';
    $grid = $this->getLayout()->createBlock('xyz_example/adminhtml_data_detail_grid');
    $this->_prepareDownloadResponse($fileName, $grid->getCsvFile());
}
public function exportXyzExcelAction()
{
    $fileName = 'example_xyz.xml';
    $grid = $this->getLayout()->createBlock('xyz_example/adminhtml_data_detail_grid');
    $this->_prepareDownloadResponse($fileName, $grid->getExcelFile($fileName));
}
}
?>

app\code\local\Xyz\Example\Block\Adminhtml\Example\Edit\Tab\Example.php

<?php
class Xyz_Example_Block_Adminhtml_Example_Edit_Tab_Example extends         Mage_Adminhtml_Block_Widget_Form
{

protected function _prepareCollection()
{
  $collection = Mage::getModel('example/example')->getCollection();
  $this->setCollection($collection);
//  return parent::_prepareCollection();
    $this->setCollection($collection);
    parent::_prepareCollection();
    return $this; 
}

protected function _prepareForm()
{
    $eId = (int)$this->getRequest()->getParam('id');
    $model = Mage::getModel('example/example')->load($eId);

    $eid = $model->getExampleId();
    $fname = $model->getFname();
    $lname = $model->getLname();
    $date = $model->getDate();
    $email = $model->getEmail();
    //print_R($data);

    $form = new Varien_Data_Form();
    $this->setForm($form);
    $fieldset = $form->addFieldset('example_form_feildset',array('legend'=>Mage::helper('example')->__('Item information')));

    $fieldset->addField('ExampleId', 'hidden', array(
      'label'     => Mage::helper('example')->__('Example Id'),
      'class'     => 'required-entry',
      'required'  => true,
      'name'      => 'title',
      'value'     => $eid,
    ));  

    $fieldset->addField('FirstName', 'text', array(
      'label'     => Mage::helper('example')->__('First Name'),
      'class'     => 'required-entry',
      'required'  => true,
      'name'      => 'title',
      'value'     => $fname,
    ));

    $fieldset->addField('LastName', 'text', array(
      'label'     => Mage::helper('example')->__('Last Name'),
      'class'     => 'required-entry',
      'required'  => true,
      'name'      => 'title',
       'value'    => $lname,
    ));

    $fieldset->addField('Email', 'text', array(
      'label'     => Mage::helper('example')->__('Email'),
      'class'     => 'required-entry',
      'required'  => true,
      'name'      => 'title',
       'value'    => $email,
    ));


    //return $eid;
    return parent::_prepareForm();
}
}
?>

app\code\local\Xyz\Example\Block\Adminhtml\Example\Edit.php

<?php
class Xyz_Example_Block_Adminhtml_Example_Edit extends  Mage_Adminhtml_Block_Widget_Form_Container
{
public function __construct()
{
    parent::__construct();

    $this->_objectId = 'id';
    $this->_blockGroup = 'example';
    $this->_controller = 'adminhtml_example';

    $this->_updateButton('save', 'label', Mage::helper('example')->__('Save'));
    $this->_updateButton('delete', 'label', Mage::helper('example')->__('Delete'));

    $this->_addButton('saveandcontinue', array(
        'label'     => Mage::helper('adminhtml')->__('Save And Continue Edit'),
        'onclick'   => 'saveAndContinueEdit()',
        'class'     => 'save',
    ), -100);
}
public function getHeaderText()
{
    return Mage::helper('example')->__('My Form Container');
}
}
?>

app\code\local\Xyz\Example\Block\Adminhtml\Example\Edit\Form.php

<?php
class Xyz_Example_Block_Adminhtml_Example_Edit_Form extends      Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
  $form = new Varien_Data_Form(array(
                                  'id' => 'edit_form',
                                  'action' => $this->getUrl('*/*/post', array('id' =>   $this->getRequest()->getParam('id'))),
                                  'method' => 'post',
                                  'enctype' => 'multipart/form-data'
                               )
  );
  $form->setUseContainer(true);

  $this->setForm($form);
  return parent::_prepareForm();
}
}
//$this->getBaseUrl().'example/index/post';
?>

app\code\local\ Xyz\Example\Block\Adminhtml\Example\Edit\Tabs.php

<?php

class Xyz_Example_Block_Adminhtml_Example_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs
{
  public function __construct()
  {
      parent::__construct();
      $this->setId('example_tabs');
      $this->setDestElementId('edit_form'); // this should be same as the form id define above
      $this->setTitle(Mage::helper('example')->__('Product Information'));    
  }
  protected function _beforeToHtml()
  {

      $this->addTab('example_section', array(
          'label'     => Mage::helper('example')->__('Account Information'),
          'title'     => Mage::helper('example')->__('Account Information'),
         'content'   => $this->getLayout()->createBlock('example/adminhtml_example_edit_tab_example')->toHtml()
      ));
      $this->addTab('example_section1', array(
          'label'     => Mage::helper('example')->__('Item Information1'),
          'title'     => Mage::helper('example')->__('Item Information1'),
          //'content'   => $this->getLayout()->createBlock('example/adminhtml_example_edit_tab_example')->toHtml()
      ));
      $this->addTab('example_section2', array(
          'label'     => Mage::helper('example')->__('Item Information2'),
          'title'     => Mage::helper('example')->__('Item Information2'),
          //'content'   => $this->getLayout()->createBlock('example/adminhtml_example_edit_tab_example')->toHtml()
      ));

      return parent::_beforeToHtml();
  }
}
?>

Best Answer

The saveAction() method (or the method you send the for to, if it's an other one) should look like this:

public function saveAction(){
    if ($data = $this->getRequest()->getPost()) {
        //init model and set data
        $model = Mage::getModel('example/example');
        if ($id = $this->getRequest()->getParam('id')) {//the parameter name may be different
            $model->load($id);
        }
        $model->addData($data);
        try{
            //try to save it
            $model->save();
            Mage::getSingleton('adminhtml/session')->addSuccess('Saved');
            //redirect to grid.
            $this->_redirect('*/*/'); 
        }
        catch (Exception $e){
            //if there is an error return to edit
            Mage::getSingleton('adminhtml/session')->addError('Not Saved. Error:'.$e->getMessage());
            Mage::getSingleton('adminhtml/session')->setExampleFormData($data);
            $this->_redirect('*/*/edit', array('id'=>$mode->getId(), '_current'=>true)); 
        }
    }        
}