如何通过codeigniter(view->controller->model)在mysql中插入multiselect下拉值

brvekthn  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(356)

view.php:-使用jquery multiselect

<form class="form-horizontal" id="user_project" action="" method="POST" enctype="multipart/form-data">
<div class="row">
 <label class=" col-sm-3 col-form-label">Select Project Members :-</label>
 <div class="col-sm-8 form-group bmd-form-group">
  <select class="form-control" name="project_members[]" multiple 
     id="project_members">
   <?php 
    foreach($members as $mem){?>
    <option value="<?php echo $mem->email;?>"><?php echo $mem->firstname." 
     ".$mem->lastname;?></option>
   <?php }?>
  </select>
 </div>
 <label class="col-sm-1 label-on-right">
  <code>*</code>
 </label>
</div>

<button type="button" class="btn btn-rose" onclick="return 
  validate_Project();"> Add Project<div class="ripple-container"> </div> 
</button>
</form>

scripting.js:-用于验证和ajax调用

function validate_Project()
{
var level = document.getElementById("project_privacy").value;
var title = document.getElementById("project_title").value;
var domain = document.getElementById("project_domain").value;
var description = document.getElementById("project_description").value;
var guide = document.getElementById("project_guide").value;
var mem = document.getElementById("project_members").value;
var status = document.getElementById("project_status").value;
var category = document.getElementById("project_category").value;

var url;
if(save_method == 'update')
{
    url = "http://localhost/pccoespii/index.php/ricontroller/edit_project/";
}
else
{
    url = "http://localhost/pccoespii/index.php/ricontroller/float_project/";
}
//pass data to database using AJAX
$.ajax({
    url : url,
    type: "POST",
    data: $('#user_project').serialize(),
    dataType: "JSON",
    success: function(data)
    {
        //if success close modal and reload ajax table
        demo.showSwal('success-message');
    },
    error: function (xhr, ajaxOptions, thrownError)
    {
        alert(thrownError);
        console.warn(xhr.responseText);
    }
});

}

控制器.php
这就是我想从视图中访问multiselect的值的地方,这样我就可以将这些值向前推到model中,从而在mysql数据库中。我也无法追踪控制器正在呈现的输出。是否需要通过循环将值推入数据库?另外,请帮助我在我应该如何继续发送这个数据到模型

public function float_project()
{
    $members = $this->input->post('project_members');
    foreach($members as $row)
    {
        $data= array(
            'project_members' => $row
        );
        echo(string($data));
    }
}
mctunoxg

mctunoxg1#

您的控制器无法读取post数据,因为您的窗体没有使用具有值post到其方法属性的窗体标记进行 Package 。试着用这个 Package 你的表格。

<div class="col-sm-8 form-group bmd-form-group">
   <form action="" method="post"> 
       <select class="form-control" name="project_members[]" multiple 
 id="project_members">
         <?php foreach($members as $mem){ ?>
            <option value="<?php echo $mem->email;?>"><?php echo $mem->firstname." 
 ".$mem->lastname;?></option>
         <?php }?>
       </select>
   </form>
</div>

你的控制器现在应该是

<?php 
    public function float_project(){
       $members = $this->input->post('project_members');
       foreach($members as $row){
        echo $row;
       } 
    } 
?>

如果您想执行insert foreach记录,您的控制器应该

<?php 
    public function float_project(){
       $members = $this->input->post('project_members');
       foreach($members as $row){
        $insert = $this->my_model->insertProjectMembers($row);
        if($insert){
           echo "Record were added";
         }
       } 
    } 
?>

你的模特应该是这样的

class My_model extends CI_Model {
    public function insertProjectMembers($row){
      $data = array(
         'db_column_name' => $row
      );
      return $this->db->insert('your_table_name', $data);
    }
}

相关问题