Wednesday, February 27, 2013

Js helper using foreach loop.

I have an app the request several product and puts them in a foreach loop list. Refer to the first image below.

Each row is a form.

 When i Click ADD everything works Great, except!,! it creates a empty data base table row.  For some reason the data is not getting passed to the data base table. Refer to the second image below.

Here is my view code:
        <h2>Products</h2>
       
    <table>
        <tr>
            <th><?php echo 'ID'; ?></th>
            <th><?php echo 'Name'; ?></th>
            <th><?php echo 'Price'; ?></th>
            <th><?php echo 'QTY'; ?></th>
            <th><?php echo 'ADD ITEM' ?></th>
        </tr>
        <?php foreach ($products as $product): ?>
        <?php echo $this->Form->create('Item'); ?>
        <tr>
            <td><?php echo $product['Product']['id']; ?></td>
            <?php echo $this->Form ->input('product_id', array('type' => 'hidden', 'value' => $product['Product']['id'])); ?>
            <td><?php echo $product['Product']['name']; ?></td>
            <?php echo $this->Form ->input('description', array('type' => 'hidden', 'value' => $product['Product']['name'])); ?>
            <td><?php echo $product['Product']['price']; ?></td>
            <?php echo $this->Form ->input('price', array('type' => 'hidden', 'value' => $product['Product']['price'])); ?>
            <td><?php
                $qtyType = array(
                    '1' => '1',
                    '2' => '2',
                    '3' => '3',
                    '4' => '4',
                    '5' => '5',
                    '6'    => '6',
                    '7' => '7',
                    '8' => '8',
                    '9' => '9',
                    '10' => '10'
                    );
               echo $this->Form->input('qty', array('class' => 'input-small','label' => FALSE, 'type' => 'select', 'options' => $qtyType));
            ?>
          </td>
          <td>
              <?php echo $this->Form->input('customer_id', array('type' => 'hidden', 'value' => $custmerID)); ?>
                <?php echo $this->Form->input('order_id', array('type' => 'hidden', 'value' => $orderID)); ?>
                <?php echo $this->Js->submit('ADD', array(
                    'before' => $this->Js->get('#sending')->effect('fadeIn'),
                    'success' =>  $this->Js->get('#sending')->effect('fadeOut'),
                    'update' => '#success'
                )); ?>
                <?php echo $this->Form->end(); ?>   
          </td>
        </tr>
        <?php endforeach; ?>
    </table>

Here is my controller code:

/* add method */
    public function add() {
        $customerNo = $this->request->params['pass'][0];
        $orderID = $this->request->params['pass'][1];
        $this->loadModel('Customer');
        $custName = $this->Customer->field('name', array('id' => $customerNo));
        $this->set('customerName', $custName);
        $this->loadModel('Order');
        $orderID = $this->Order->getLastInsertID();
        ///////////////// this is where the magic happens
        if ($this->request->is('post')) {
            $this->Item->create();
            if ($this->Item->save($this->request->data)) {
                if($this->RequestHandler->isAjax()) {
                    $this->render('success', 'ajax');
                }else{
                $this->Session->setFlash(__('The item has been saved'));
                $this->redirect(array('action' => 'index'));
                }
            } else {
                $this->Session->setFlash(__('The item could not be saved. Please, try again.'));
            }
        }
        $order = $this->Item->Order->find('list');
        $this->set(compact('order'));
        $this->loadModel('Product');

        $keyValue = $this->Product->find('all', array(
            'fields' => array('id', 'name', 'price')
            ));
        $this->set('custmerID', $this->request->params['pass'][0]);
        $this->set('products', $keyValue);
        $this->set('orderID', $this->request->params['pass'][1]);
    }

 Any Help on what i am doing wrong would be a great help.


--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: