Welcome Guest, Not a member yet? Register   Sign In
Modular Extension - passing data from view to controller via form
#1

[eluser]ice_prince7[/eluser]
[EDIT - sorry for double posting. it should be here : http://ellislab.com/forums/viewthread/73177/P690/ ]

Hi all,

sorry to bother you with this might-be-something-obvious problem

Im trying out Modular extension by wiredesignz.

I got stumble when i want to pass data via form_open.

it seems that it doesnt go to my immediate controller, but rather to index.php/product/add_to_cart.

This is my hierachy
Quote:application/
modules/
product/
controllers/product.php
views/product_list.php

This is my controller
Code:
<?php

class Product extends Controller {

    function __construct()
    {
        parent::Controller();
    }
    
    function product_list()
    {  
        #get products
        $get_product = $this->db->get('product');
        $data['product'] = 'empty';
        if($get_product->num_rows()>0){
        
            $data['product']=$get_product->result();  
            
        }
        
        $this->load->view('product_list',$data);
    }
    
    function add_to_cart(){
        echo 'asdsadas';
        print_r($this->input->post('product'));
        
        
    }
}

/* End of file product.php */
/* Location: ./application/modules/product/controllers/product.php */

This is my view :
Code:
<div id="cart">
    <h1>Product List</h1>
    <div id="content">
        &lt;?php if($product=='empty'){
            
            ?&gt;<div>There is no product yet</div>&lt;?php
            
            
        }else{
            
            ?&gt;
            <table>
                <tr>
                    <td>Item</td>
                    <td>Quantity</td>
                    <td>Price</td>
                    <td>&nbsp;</td>
                
                </tr>
            
            &lt;?php
            
            foreach($product as $item){
                $len =20;
                ?&gt;
                      
                <tr>
                    <td>&lt;?php if(strlen($item->productName)>=$len){echo substr($item->productName,0,$len).'...';}else{echo $item->productName;}; ?&gt;</td>
                    <td>&lt;?php echo $item->quantity; ?&gt;</td>
                    <td>Price</td>
                    <td>
                        &lt;?php echo form_open('product/add_to_cart');?&gt;// here is where im passing my data
                        &lt;input type="submit" name="product[&lt;?php echo $item-&gt;productId; ?&gt;]" value="Add to Cart" />
                        &lt;?php echo form_close();?&gt;
                    </td>
                
                </tr>
                &lt;?php
            }
        ?&gt;</table>&lt;?php    
        }?&gt;
    </div>
</div>

Any ideas on how do i get the data from view to controllers/product/add_to_cart ?
sorry for posting any noob question.

thankx
#2

[eluser]Suhas nazir[/eluser]
hey you can use post method to acess data when u use form_open....
like this $this->input->post('variablename');
#3

[eluser]ice_prince7[/eluser]
@SAS :

yep, i did it in my controller (modules/product/product):
Code:
function add_to_cart(){
        echo 'asdsadas';
        print_r($this->input->post('product'));
        
        
    }

but the thing is, my form wouldnt redirect me to the add_to_cart method.
it simply went on to :
index.php/product/add_to_cart

instead of to :
/modules/product/product/add_to_cart

In a normal controller, it should work ok. Due to the fact that im trying out Modular Extension, somehow, it does not redirect properly or something i did prevent it from redirect to the proper method.

appreciate your concern, thanks for the reply Big Grin
#4

[eluser]Suhas nazir[/eluser]
Sry,Sry,I didnt go through the entire code......
#5

[eluser]coolamit[/eluser]
Instead of this
Code:
&lt;?php echo form_open('product/add_to_cart');?&gt;
have you tried this?
Code:
&lt;?php echo form_open('/modules/product/product/add_to_cart/');?&gt;
#6

[eluser]ice_prince7[/eluser]
yep, i was directed here :
http://ci/main.php/modules/product/product/add_to_cart

but i didnt get any respond from the controllers 'product' and method 'add_to_cart' that resided in :
application/modules/product/controllers/add_to_cart


The reason of having modular extension is to have a modular MVC so that it can communicate within the module.

Therefore, from my understanding, controllers and views inside the module (say in this case 'product') should be able to communicate to each other, like this :

(application/modules/product/views) <-> (application/modules/product/controllers)

instead of having to go to the main controllers and views, like this :

(application/controllers)<->(application/views)

So, by having the form, like this :

Code:
form_open('product/add_to_cart/)

it should go to :

application/modules/product/controllers/product/add_to_cart

right?

hmm, am i getting the concept right? Big Grin

anyway, thank you for the effort @coolamit and @SAS
#7

[eluser]coolamit[/eluser]
Yes it should work like you think, but CI doesn't support modules natively. Which library are you using for this implementation?
#8

[eluser]ice_prince7[/eluser]
im using modular extension ( http://codeigniter.com/wiki/Modular_Extensions_-_HMVC/ )




Theme © iAndrew 2016 - Forum software by © MyBB