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

时间:2014-11-27 04:43:43

标签: php jquery database codeigniter drop-down-menu

我是codeigniter的新手,我对如何在下拉列表中添加选项以及将下拉列表中添加的选项保存到数据库感到困惑。任何人都可以帮我这个吗?

我的观点:

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

我的控制器:

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

我的模特:

     function getAll() {

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

      }

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

如何将添加的选项保存到数据库中?

2 个答案:

答案 0 :(得分:0)

我给出简单的php答案....将其更改为ci类型

html&amp; ajax代码:

  <input type="text" name="selct" id="name" ><input type="submit" id="submit"/>
 <div id="box"> <select name="selectbox" id="testselect"></select></div>
  <script>
 $('#submit').click(function(){
 $.ajax({
          type: "POST",
           url: "some.php",// use ci url here(i.e call controller)
           data: { selectboxvalue: $("#name").val() } ///value will be sent
            })
          .done(function( msg ) {
          $('#box').html(msg);
           });
   });
   </script>

Some.php页面

   database connection

   insert $_post['selectboxvalue'] value in your database and fetch all values
   from database and populate in select box.i think it is easy so i am not writing 
   the code 

答案 1 :(得分:-1)

试试这个例子

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