如果在下拉列表中选择了某个值,Codeigniter如何添加textarea

时间:2016-02-03 06:53:34

标签: database forms codeigniter

我有一个由我的数据库填充的下拉列表,所以我想要的是如果用户想要添加另一个项目但是无法在下拉列表中找到该类别,他会选择"其他"从列表中,然后将为该特定项目创建一个新类别

视图

<!doctype html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> 

        <title>Add Item</title>

        <!-- Bootstrap -->
        <link href="css/bootstrap.min.css" rel="stylesheet">
        <link href="css/bootstrap-theme.min.css" rel="stylesheet">
        <link rel="shortcut icon" href="assets/ICON.PNG">
        <link href='https://fonts.googleapis.com/css?family=Raleway:200' rel='stylesheet' type='text/css'>

</head>

<body>
<div class="container">


    <div class="row" >


        <div class="col-md-10">
            <br>
            <div class="panel panel-default">
                <div class="panel-body">



                    <div class="panel panel-default">
                        <div class="panel-body">

                            <?php echo form_open_multipart('add_products/do_upload'); ?>



                            <center>        <?php if (validation_errors()): ?>

                                    <div class="alert alert-danger alert-dismissible" role="alert" style="width: 700px;">
                                        <?php echo validation_errors(); ?>
                                    </div>
                                <?php endif ?>


                                <div class="form-group">
                                    <label class="col-sm-2 control-label" style=" color: white"></label>

                                    <label class="col-sm-2 control-label">Product Image</label>
                                    <div class="col-sm-5">
                                        <input type="file" class="form-control" placeholder="" name="userfile">
                                    </div>

                                </div>
                                <br> <br>
                                <br>
                                 <div class="form-group">
                                    <label class="col-sm-2 control-label" style=" color: white"></label>

                                    <label class="col-sm-2 control-label">Product Name</label>
                                    <div class="col-sm-5">
                                        <input type="text" class="form-control" placeholder="ex. coffee"
                                               name="name"
                                               value="<?php echo set_value('name'); ?>">
                                    </div>

                                </div>
                                <br> <br>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label" style=" color: white"></label>

                                    <label class="col-sm-2 control-label">Description</label>
                                    <div class="col-sm-5">
                                        <input type="text" class="form-control" placeholder="ex.  brewed drink from roasted coffee"
                                               name="description"
                                               value="<?php echo set_value('description'); ?>">
                                    </div>

                                </div>
                                <br> <br>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label" style=" color: white"></label>

                                    <label class="col-sm-2 control-label">Price</label>
                                    <div class="col-sm-5">
                                        <input type="number" class="form-control" placeholder="ex. P180"
                                               name="price"
                                               value="<?php echo set_value('price'); ?>">
                                    </div>

                                </div>       
                                <br> <br>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label" style=" color: white"></label>

                                    <label class="col-sm-2 control-label">Category</label>
                                    <div class="col-sm-5">

                                 <select class="form-control" name="prod_category">
            <?php 

            foreach($category as $row)
            { 
              echo '<option value="'.$row->category.'">'.$row->category.'</option>';

            }
            ?>
                                     <option>Other</option>

            </select></div>

                                </div>    


                                <br> <br>

                                <button style="margin-left: 75px; width: 320px;" type="submit" class="btn btn-success">Submit</button>

                                </form>
                            <?php echo form_close() ?></div>

                    </div>
                </div>

            </div>

        </div>


    </div>



</div>
</div>


</body>
</html>

控制器&#34;

function index()
 {
      $this->load->helper(array('form'));
   if($this->session->userdata('logged_in'))
   {
     $session_data = $this->session->userdata('logged_in');
     $data['username'] = $session_data['username'];
     $data['category'] = $this->User->get_category();
     $this->load->view('add_products',$data);
   }

模型:

public function get_category(){
        $p = $this->db->query("select category from product_category");
        $p1 = $p->result();
        return $p1;
    }

2 个答案:

答案 0 :(得分:1)

您可以在jquery中使用change()将textarea添加到UI

$("#category").on("change",function() {
    var selectedcat = $(this).val();
    if(selectedcat == -1)
    {
        $("#newcategory").show();
    }
  });

请参阅下面的Filddle https://jsfiddle.net/5ueco3s9/

答案 1 :(得分:0)

选择框的html: -

 <select class="form-control select" name="prod_category">
                <?php 

                foreach($category as $row)
                { 
                  echo '<option value="'.$row->category.'">'.$row->category.'</option>';

                }
                ?>
                                         <option value="lllvnjs">Other</option>

                </select>
<div class="append"></div>

然后jquery改变你的选择框(别忘了在你的标记之前添加jquery)

$("select").change(function(){
   if($(this).val() == "lllvnjs")
{
   $(".append").append('<input type="text" value="" name="other_category" />');
}
});

最后提交表单时,请在控制器中检查此条件

$category = $_POST["prod_category"];
if($category  == "lllvnjs")
{
$category = $_POST["other_category"];
}

并将其插入您的数据库或者只是想做这件事:)