如何使用html下拉列表在codeigniter中将数据添加到数据库表?

时间:2017-07-20 07:24:59

标签: php html mysql codeigniter

这是我的观看代码。我需要将价格,数量,颜色和产品ID添加到表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>

我是codeigniter的新手。上面是我的代码我需要将价格,颜色,数量下拉值和产品ID插入到表tbl_cart.How它可以用html和codeigniter完成?请帮帮我。

3 个答案:

答案 0 :(得分:1)

围绕你的模态包装:

 <!-- 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>

      

在您的控制器中,即控制器名称:

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
}

答案 1 :(得分:0)

firt:将表单的字段放在<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> 之间 选择必须具有不同的名称,如示例中所示。

在您的控制器中:

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');

}

在同一方法中输入要发送到数据库的代码:

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

我希望我有所帮助。

答案 2 :(得分:0)

  

首先将您的内容放在一个表单中,然后在表单操作中指定控制器名称,然后是i控制器,例如

     

在控制器中

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);
}
  

你的模特

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

我首先在这里解释我在一个名为$ data的数组中得到所有表单字段值然后我将这个名为$ data的数组变量传递给你的模型并插入它。