Search code examples
phpcakephpshopping-cartcakephp-3.6

Values not passed from ctp file to controller in CakePHP


I have tried several solution posted in this forum and others as well but it has not helped so far. So I am posting my question finally. BTW, I am using CakePHP 3.6.

I am trying to pass a variable ($product->id) via submit button in view.ctp to my controller action "addit" but I just get "Undefined variable: id " (I have tried addit($id) and addit() either of case I have the same result.)

view.ctp

<p>
    <?php echo $this->Form->create('NULL',['url'=>['controller'=>'products','action'=>'addit']]);?>
    <?php echo $this->Form->input('id', ['type' => 'hidden', 'value' => $product->id]); ?>

    <?php echo $this->Form->button('Add to cart now');?>

    <?php echo $this->Form->end();?>

</p>

Controller:Products

public function addit() {
    $this->autoRender = false;
    if ($this->request->is('post')) {
        // $this->Products->addProduct($this->request->data['Cart']['product_id']);
        echo "".$this->Products->get($id);//for test
    } else {
        echo "".$this->Products->get($id);//for test
    }
 }

Solution

  • Is this what you want to do?

    $id = $this->request->getData('id');
    debug($this->Products->get($id)); //for test