3

I have created the module with a form in front end on submit it send email and its working fine, now i also want to save the current form data to a Database table and keep the email functionality too that is working fine right now:

Here is the form action:

<form id="simple_contact_form" name="simple_contact_form" action="<?php echo $this->getUrl('reviews/email/sendemail') ?>" method="post">

And here the function to in my module to send email:

       <?php
        class OptFirst_ReviewMyCompany_EmailController extends
        Mage_Core_Controller_Front_Action
        {
        public function sendemailAction()
        {
         $params = $this->getRequest()->getParams();
          $mail = Mage::getModel('core/email');
    $body = '<p>Hello,</p>
    <p>A New CLient Review Is Received:</p>
    <p><b>Name:</b> ' . ($params['name']) . '<br>
    <p><b>Email:</b> ' . ($params['email']) . '<br>
    <b>Comments:</b> ' . ($params['comment']) . '</p>';
    $mail = new Zend_Mail();
    $mail->setBodyHtml($body);
    $mail->setFrom($params['email'], $params['name']);
    $mail->addTo('techleadz.wpteam@gmail.com');
    $mail->setSubject('Review Form');

    try {

        $mail->send();
        Mage::getSingleton('core/session')->addSuccess('Message has been Sent Successfully.');
        $this->_redirect('reviews/');
         }

    catch (exception $ex) {
        Mage::getSingleton('core/session')->addError('Unable to send email. Sample of a custom notification error from Contact Us Form.');

    }
     $connectionresource = Mage::getSingleton('core/resource');
$connectionWrite = $connectionresource->getConnection('core_write');
$table = 'optfirst_contacts';
$query = "insert into ".$table." "
                 . "(name,email,comments) values "
                 . "(:name, :email, :comments)";

          $binds = array(

              'name'   => 'values',
              'email' => 'values',
              'comment' => 'values',

          );

          $connectionWrite->query($query, $binds);
    $this->_redirect('reviews/');

   }
  }

created the query to save data to DB table but it throws an error:

Invalid parameter number: parameter was not defined

What is wrong going on here any help?

Qaisar Satti
  • 32,469
  • 18
  • 85
  • 137
Xabby
  • 529
  • 6
  • 18
  • You need to create model and table in database (setup script) after that you can save data from your form like example $params = your post info, $model = Mage::getModel('custom/custom'); $mode->setData($params); $model->save(); – omelandr Dec 19 '16 at 15:53
  • But the form action is currently going to reviews/email/sendemail' so how the form will submited to Database? – Xabby Dec 19 '16 at 15:55
  • As above - you should add some table via sql script and then save data using model. if in that action you ar lacking of any info you can add some hidden inputs. I there is a provlem where request goes you can change url for form and create custom controller or perform db save in observer on some event – Bartosz Kubicki Dec 19 '16 at 18:13
  • Any demo work or tutorial will be more helpful @lord_of_strings – Xabby Dec 19 '16 at 18:32
  • Check how events and observers works - you can find it in google. Also tutorials about creating a table in db and models which serves you for adding data can be easily found. First prepare table, than model and after that you can add saving data in your action. If table you want to save your data to is not custom one and already exists you can use it right now and needs coding only strictly for saving operation. – Bartosz Kubicki Dec 20 '16 at 08:08

1 Answers1

3

you can send custom email with below code.

    $mail = Mage::getModel('core/email');
    $body = '<p>Hello,</p>
    <p>A New CLient Review Is Received:</p>
    <p><b>Name:</b> ' . ($params['name']) . '<br>
    <p><b>Email:</b> ' . ($params['email']) . '<br>
    <b>Comments:</b> ' . ($params['comment']) . '</p>';
    $mail->setBody($body);
    $mail->setFromEmail($params['email']);
    $mail->setFromName($params['name']);
    $mail->setToEmail('techleadz.wpteam@gmail.com');
    $mail->setSubject('Review Form');
    $mail->setType('html');
    try {
      $mail->send();
        Mage::getSingleton('core/session')->addSuccess('Your request has been sent');
        $this->_redirect('');
       }
    catch (Exception $e) {
       Mage::getSingleton('core/session')->addError('Unable to send.');
       $this->_redirect('');
      }

check as a reference

simple way to add in database

    $connectionresource = Mage::getSingleton('core/resource');
    $connectionWrite = $connectionresource->getConnection('core_write');
    $table = 'test';
    $query = "insert into ".$table." "
                     . "(test_id,content, abc) values "
                     . "(:test_id, :content, :abc)";

              $binds = array(
                  'test_id'    => 1,
                  'content'   => 'values',
                  'abc' => 'vlues',

              );

              $connectionWrite->query($query, $binds);

for model use this

Qaisar Satti
  • 32,469
  • 18
  • 85
  • 137