View.php:-使用了jquery多项选择
<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);
}
});
}
Controller.php
在这里我要从视图访问multiselect的值,以便可以将这些值向前推送到模型,从而在MySql数据库中进行推送。 我也无法跟踪控制器正在渲染什么输出。 是否需要通过循环将值推入数据库? 另外,请帮助我如何继续将此数据发送到模型
public function float_project()
{
$members = $this->input->post('project_members');
foreach($members as $row)
{
$data= array(
'project_members' => $row
);
echo(string($data));
}
}
答案 0 :(得分:0)
您的控制器无法读取发布数据,因为您的表单没有被表单标签包裹,该表单标签的方法属性具有发布值。尝试用此包装表单。
<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;
}
}
?>
如果要对每个记录执行插入操作,则您的控制器应该为
<?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);
}
}