0

I'm currently making a newsletter that let users subscribe and unsubscribe. Subscribing is a success but not on unsubscribing. can you help me out guys, here's my codes...

Controller

class AddsController extends AppController {
    public $helpers = array('Html','Form','Session','Flash');
    public $components=array('Session');

    public function index() {

        if($this->request->is('post')) //posting to db
        {
            $this->Add->create();
            $this->Add->save($this->request->data);
            $this->Flash->set('The user has been saved.', array('element' => 'success'));
            return $this->redirect('index'); //redirects to index.ctp
        }
    }

    /*public function delete($email = null) {
        if($this->request->is('post')) {
            $this->Add->exists($email) {
                $this->Add->delete($email);
                $this->Session->Flash('Unsubscribed');
                return $this->redirect('index');
            }
        }

    function delete(){ //delete html
        $email = $this->request->data['email'];
        if($this->Add->exists($email)){
            $this->Add->delete($email);
            $this->Session->Flash('Unsubscribed');
            //$this->Flash->set('Unsubscribed', array('element' => 'danger'));
            return $this->redirect('index');
        }
    }   */
    function delete() {
    $email = $this->request->data['email'];
    if($this->Add->exists($email)){
        $this->Add->delete($email);
        $this->Session->Flash('Unsubscribed');
        //$this->Flash->set('Unsubscribed', array('element' => 'danger'));
        return $this->redirect('index');
    }
}

}

View

<?php echo "<section>";
echo $this->Form->create('Add'); //<form > 'modelname'
echo "<label>E-mail:</label>";
echo $this->Form->input('email',array('label'=>false , 'placeholder'=>'your email')); //email input
echo $this->Form->submit('Subscribe'); //subscrib button
echo $this->Form->end();//</form>
echo "</section>";

echo "<section>";
echo $this->Form->delete('Add',array('action'=>'delete')); //<form > 
echo "<label>E-mail:</label>";
echo $this->Form->input('email',array('label'=>false , 'placeholder'=>'your email')); //email input
echo $this->Form->submit('Unsubscribe',array('action'=>'index'),array('style'=>'background:red'));
echo $this->Form->end();//</form>
echo "</section>";

My view is just "entering data into db and deleting existed data out the db Model

<?php

App::uses('AppModel' , 'Model');
class Add extends AppModel
{
    public $name = "Add";
    public $useTable = 'request';
    public $primaryket = 'id';
    public $useDbConfig = 'default';
}

db tablename: request; Just the delete row Thank you in advance!!!

1
  • without using another .ctp Commented Sep 20, 2017 at 7:01

2 Answers 2

1

try to use below method (pass unique id of user) hope it will work:

$user = $this-> Add->findByEmail($email);
$this-> Add->delete($user['User']['id'], false);
Sign up to request clarification or add additional context in comments.

Comments

0

Alternative way

 $userId = $this->User->field('id', array('User.email' => $email));
 if($userId) {
  $this->User->id = $userId;
  $this->User->delete();
  $this->log('User of '.$email.' record deleted successfully');
  } else {
   $this->log('There is no record present of particular email = '. $email); 
  }

1 Comment

It actually work but I replaced the $this->log() into $this->set(). Still, thanks for the help Sir @Harshal Shinde

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.