douyuefei3546 2014-11-27 04:43
浏览 55

如何添加选项以下拉列表并将其保存到数据库?

I'm new to codeigniter and I'm really confused on how to add options to the drop down list and save the added options in the drop down list to the database. Can anyone help me with this?

My View:

<!doctype html>
 <html>
  <head>
   <title>My Form</title>
    <script src="<?php echo base_url();?>assets/js/jquery-1.11.1.js" type="text/javascript"></script>
  <script>

  $(document).ready(function() {
  $('#myselect').change(function(e) {
    if ($(this).val() == "addother") {

        $('#addother').show();

        $('#addother_input').val('');

    } else {
        $('#addother').hide();
    }
    });

      $('#add').click(function() {
               $.ajax({
                    type: "POST",
                     url: window.location,// use ci url here(i.e call controller)
                     data: { selectboxvalue: $options.val() } ///value will be sent
                      })
                    .done(function( msg ) {
                    $('#add').html(msg);
                     });
             });

});

  </script>
  <style>
     #addother {
     display: none;
     }

   </style>
 </head>


 <body>

 <div id="container">
    <?php 
       $options = array(
              'small'  => 'Small Shirt',
              'med'    => 'Medium Shirt',
              'large'   => 'Large Shirt',
              'xlarge' => 'Extra Large Shirt',
              'addother' => "Add other..."
            );

       echo form_dropdown('shirts', $options, null, 'id="myselect"');

     ?>
      <div id="addother">
        <?php  echo form_input(array('id'=>'addother_input', 'name'=>'add', 'placeholder'=>'Enter name of school...')); ?>
        <input type="submit" id="add" name="submit" value="+" />
       </div>
    </div>

</body>
</html>

My Controller:

    function add() {
         $this->load->model('fruit_model');

         $data['selectboxvalue'] = $this->input->post('selectboxvalue');

         $res = $this->model->addItem($data);

         $this->load->view('myform');
     }

    function drop() {

         $this->load->model('getAll');

         $data["opt"] = $this->fruit_model->getAll();

         $this->load->view('myform');
     }

My Model:

     function getAll() {

       $query = $this->db->get('fruits')
       return $query->result();

      }

     function addItem($data){
         $this->db->insert('fruits', $data);
         return;
      }

How to save the added options into the database?

  • 写回答

2条回答 默认 最新

  • douni9620 2014-11-27 05:26
    关注

    try this example

    <?php
    
    class Options extends CI_Controller{
    
    function index()
    {
        $this->load->view('view name');
    
    }
    
    function create()
    {
        $data = array(
    
            'name' => $this->input->post('name'),
            'price' => $this->input->post('price'));
    
        $this->data_model->addItem($data);
        $this->index();
    }
    
    }
    
    Model
    
    <?php
    
    class Data_model extends CI_Model {
    function getAll() {
    $q = $this->db->query("SELECT * FROM items");
    if($q->num_rows() >0){
        foreach($q->result() as $row){
            $data[]=$row;
        }
    }
    return $data;
    }
    
    function addItem($data){
        $this->db->insert('items', $data);
        return;
    }
    }
    ?>
    
    View
    
    <html><head></head><body>
    <style type="text/css">
    label {display: block;}
    </style>
    
    
    <h2>Create</h2>
    <?php echo form_open('controllername/function name/'); ?>
    
    <p>
        <label for="name">Name</label><input type="text" name="name" id="name" />
    </p>
    <p>
        <label for="name">Price</label>
        <select name="price">
          <option value="1">$100</option>
          <option value="2">$200</option>
          <option value="3">$300</option>
        </select>
    </p>
    
    <p><input type="submit" value="Submit" /></p>
    
    <?php echo form_close(); ?>
    
    </body>
    </html>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?