EjM
EjM

Reputation: 103

How to add data to a database table in codeigniter using html drop down?

Here is my view code. I need to add price,qty,color and product id to the table tbl_cart .

<div class="modal fade product_view" id="product_view">
   <div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <a href="#" data-dismiss="modal" class="class pull-right"><span 
       class="glyphicon glyphicon-remove"></span></a>
            <h3 class="modal-title"> School Zone</h3>
        </div>
        <div class="modal-body">
            <div class="row">
                <div class="col-md-6 product_img">
                    <img src="<?php echo site_url()?>images/offer_gallery/1.jpg" class="img-responsive">
                </div>

                    <p>These packs are absolutely great for any activity or adventure you have planned!</p>
                    <h3 class="cost"><span class="glyphicon glyphicon-usd"></span> 19.99 <small class="pre-cost"><span class="glyphicon glyphicon-usd"></span> 22.00</small></h3>
                    <div class="row">
                        <div class="col-md-4 col-sm-6 col-xs-12">
                            <select class="form-control" name="select">
                                <option value="" selected="">Color</option>
                                <option value="black">Black</option>
                                <option value="navy">Navy</option>
                                <option value="gray">Gray</option>
                                <option value="blue">Blue</option>
                                <option value="charcoal">Charcoal</option>
                            </select>
                        </div>


                        <!-- end col -->
                        <div class="col-md-4 col-sm-12">
                            <select class="form-control" name="select">
                                <option value="" selected="">QTY</option>
                                <option value="1">1</option>
                                <option value="2">2</option>
                                <option value="3">3</option>
                                <option value="4">4</option>
                                <option value="5">5</option>
                            </select>
                        </div>
                        <!-- end col -->
                    </div>
                    <div class="space-ten"></div>
                    <div class="btn-ground">
                        <button type="button" class="btn btn-primary"><span class="glyphicon glyphicon-shopping-cart"></span> Add To Cart</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

Am new to codeigniter.. Above is my code i need to insert price,color,qty drop down value and product id to the table tbl_cart.How it can done with html and codeigniter ? Please help me.

Upvotes: 1

Views: 555

Answers (3)

Parker Dell
Parker Dell

Reputation: 468

Around your modal wrap:

 <!-- Form Tags Using Ci helper class-->
<?php echo form_open('controller_name/method_you_are_posting_to', array('method'=>'POST', 'class'=>'form-horizontal', )); ?>

<div class="modal fade product_view" id="product_view">
 <div class="modal-dialog">
  <div class="modal-content">
    <div class="modal-header">
        <a href="#" data-dismiss="modal" class="class pull-right"><span 
   class="glyphicon glyphicon-remove"></span></a>
        <h3 class="modal-title"> School Zone</h3>
    </div>
    <div class="modal-body">
        <div class="row">
            <div class="col-md-6 product_img">
                <img src="<?php echo site_url()?>images/offer_gallery/1.jpg" class="img-responsive">
            </div>

                <p>These packs are absolutely great for any activity or adventure you have planned!</p>
                <h3 class="cost"><span class="glyphicon glyphicon-usd"></span> 19.99 <small class="pre-cost"><span class="glyphicon glyphicon-usd"></span> 22.00</small></h3>
                <div class="row">
                    <div class="col-md-4 col-sm-6 col-xs-12">
                         <!-- Need to give unique name to select -->
                        <select class="form-control" name="color">
                            <option value="" selected="">Color</option>
                            <option value="black">Black</option>
                            <option value="navy">Navy</option>
                            <option value="gray">Gray</option>
                            <option value="blue">Blue</option>
                            <option value="charcoal">Charcoal</option>
                        </select>
                    </div>


                    <!-- end col -->
                    <div class="col-md-4 col-sm-12">
                        <!-- Need to give unique name to select -->
                        <select class="form-control" name="quantity">
                            <option value="" selected="">QTY</option>
                            <option value="1">1</option>
                            <option value="2">2</option>
                            <option value="3">3</option>
                            <option value="4">4</option>
                            <option value="5">5</option>
                        </select>
                    </div>
                    <!-- end col -->
                </div>
                <div class="space-ten"></div>
                <div class="btn-ground">
                    <!-- Change button to be a submit button, also give it a name -->
                    <button type="submit" name="submit_button" value="1" class="btn btn-primary"><span class="glyphicon glyphicon-shopping-cart"></span> Add To Cart</button>
                </div>
            </div>
        </div>
    </div>
</div>

In your controller ie controller name do:

public function method_you_are_posting_to(){

  //if there is post data
  if($this->input->post('submit_button'){
      //info you want to upload to db  
      //get values from the post using ci helpers       
      $data = [
          'color'=>$this->input->post('color'),
          'quantity'=>$this->input->post('quantity'),
      ];

     //to update to db, this should be moved to a model call
     $this->db->insert('table_name', $data);
  }
  //you will want to redirect somewhere then
}

Upvotes: 1

dipti
dipti

Reputation: 71

First put your content in a form,then in form action assign controller name, then i controller ,like-example

In controller

public function yourcontroller(){
$data = array('your column name' => $this->input->post('select'),'your 
column name' => $this->input->post('select'));
$this->your model name->your_model_function_name($data);
}

Your model

function your_model_function_name($data){
$this->db->insert('your_table_name',$data);
}

I explain here first i get all form field value in an array called $data then i pass this array variable called $data into your model and insert it.

Upvotes: 0

Nukemp
Nukemp

Reputation: 26

firt: Place the fields of your form between the <form action="<?php echo site_url("your_controller/method");?>" method="post"> <select class="form-control" **name="color"**> </select><select class="form-control" **name="qty"**></select> <button **type='submit'**>send</button> </form> The selects must have different names like in the examples.

in your controller:

public function method(){
    $data['column1_in_your_dataBase'] = $this->input->post('name_select_1');
    $data['column2_in_your_dataBase'] = $this->input->post('name_select_2');

}

in the same method enter the code to send to the database:

 $this->db->insert("Database_name",$data);

I hope I have helped.

Upvotes: 0

Related Questions