I need to use the form once in the footer and on a separate page, i.e. index.ctp . For this, I have a database table called contactforms . I created a Contactform.php model
<?php App::uses('AppModel', 'Model'); class Contactform extends AppModel { var $useTable = false; public $validate = array( 'firstname' => array( 'notempty' => array( 'rule' => array('notempty') ) ), 'contactno' => array( 'notempty' => array( 'rule' => array('notempty') ) ), 'email' => array( 'notempty' => array( 'rule' => array('notempty') ) ) ); } ?>
I have a controller from where I'm trying to send an email
<?php App::uses('AppController', 'Controller'); App::uses('CakeEmail', 'Network/Email'); class ContactformsController extends AppController { public function index() { $this->Contactform->recursive = 0; $this->set('contactforms', $this->paginate()); } public function contact() { $email = new CakeEmail(); if(isset($this->params['requested']) && $this->params['requested']==true) { if ($this->request->is('post')) { $this->Contactform->set($this->request->data); if($this->Contactform->save($this->request->data)) { $name=$this->request->data['Contactform']['firstname']; $lastname=$this->request->data['Contactform']['lastname']; $contact=$this->request->data['Contactform']['contactno']; $mail= $this->request->data['Contactform']['email']; $email->from(array($mail => $name)); $email->to(' abc@gmail.com '); $message= $this->request->data['Contactform']['message']; $email->subject('Wombats contact form information'); if($email->send($message)) { $this->Session->setFlash('Quote Processed..Thank You For Visiting Our Website!!!'); $this->redirect($this->referer()); } } } } } } ?>
And then I created an element that I used in the footer and then in the index file. contact.ctp looks like
<?php echo $this->Html->css('contactfooter.css');?> <?php $contactforms = $this->requestAction('Contactforms/contact') ?> <div class="frm"> <?php echo $this->Form->create('Contactform'); ?> <div class="firstrow"> <div class="first"> <?php echo $this->Form->input('firstname',array('label'=>false,'placeholder'=>'firstname','div'=>'firstname','style'=>'width:130px; height:20px;' ));?> <?php </div> <div class="second"> <?php echo $this->Form->input('lastname',array('label'=>false,'placeholder'=>'lastname','div'=>'lastname','style'=>'width:140px; height:20px; '));?> </div> </div> <?php echo $this->Form->input('contactno',array('label'=>false,'placeholder'=>'contactno','div'=>'contactno','style'=>'width:270px; height:20px; margin-bottom:10px;'));?> <?php echo $this->Form->input('email',array('label'=>false,'placeholder'=>'email','div'=>'email','style'=>'width:270px; height:20px; '));?> <?php echo $this->Form->input('message',array('label'=>false,'placeholder'=>'message','div'=>'message','style'=>'width:270px; height:25px;margin-top:10px; '));?> </div> <div class="sub"> <?php echo $this->Form->end('SUBMIT'); ?> </div>
When I click the submit button of one form, another form is also checked. I tried a lot, but donβt know how to fix it. Someone can help.
EDIT: - @Nunser I am very confused by these names, sorry for that. I changed my code according to what I said, but still checked only one form. I have doubts, according to you, I have to change my mind and elements, but I just have elements. Please help me in publishing my edited code.
I named the item from the index page
<?php echo $this->element('Contactform/contact',array('source'=>'index')); ?>\
and from the default page
<?php echo $this->element('Contactform/contact'); ?>
my controller action
public function contact() { $email = new CakeEmail(); if(isset($this->params['requested']) && $this->params['requested']==true){ if ($this->request->is('post')) { $index = 'Contactform'; if (isset($this->request->data['Contactformindex'])) $index = 'Contactformindex'; $this->Contactform->set($this->request->data[$index]); if($this->Contactform->save($this->request->data[$index])) { $name=$this->request->data[$index]['firstname']; $lastname=$this->request->data[$index]['lastname']; $contact=$this->request->data[$index]['contactno']; $mail= $this->request->data[$index]['email']; $email->from(array($mail => $name)); $email->to(' skyhi13@gmail.com '); $message= $this->request->data[$index]['message']; $email->subject('Wombats contact form information'); //$email->send($message); if($email->send($message)) { $this->Session->setFlash('Quote Processed..Thank You For Visiting Our Website!!!'); //$this->render('/view/elements/quotes/quoteform.ctp'); // $this->autoRender=FALSE; $this->redirect($this->referer()); } } else { $this->set('formName',$index); } } } }
In the ctp file of the elements I changed as
<?php if (!empty($this->validationErrors['Contactform'])) { $this->validationErrors[$formName] = $this->validationErrors['Contactform']; }?> <div class="frm"> <?php if(isset($source)&& $source == 'index') echo $this->Form->create('Contactformindex'); else echo $this->Form->create('Contactform'); ?> <div class="firstrow"> <div class="first"> <?php echo $this->Form->input('firstname',array('label'=>false,'placeholder'=>'firstname','div'=>'firstname','style'=>'width:130px; height:20px;' ));?> <?php </div> <div class="second"> <?php echo $this->Form->input('lastname',array('label'=>false,'placeholder'=>'lastname','div'=>'lastname','style'=>'width:140px; height:20px; '));?> </div> </div> <?php echo $this->Form->input('contactno',array('label'=>false,'placeholder'=>'contactno','div'=>'contactno','style'=>'width:270px; height:20px; margin-bottom:10px;'));?> <?php echo $this->Form->input('email',array('label'=>false,'placeholder'=>'email','div'=>'email','style'=>'width:270px; height:20px; '));?> <?php echo $this->Form->input('message',array('label'=>false,'placeholder'=>'message','div'=>'message','style'=>'width:270px; height:25px;margin-top:10px; '));?> </div> <div class="sub"> <?php echo $this->Form->end('SUBMIT'); ?> </div>
Using this code, he still checked only one form, and the form is what I called without source , like index , and when I click on the index submit button it checks the other form. I'm not sure how I need to use the same Fromindex name as you specify, it doesn't matter. I can not find where I am wrong. Please help and thanks in advance.