firstname." ".$mem--6ren">
gpt4 book ai didi

php - 如何通过codeigniter在mysql中插入多选下拉值( View -> Controller ->模型)

转载 作者:行者123 更新时间:2023-11-29 16:45:00 25 4
gpt4 key购买 nike

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

这是我想要从 View 访问多选值的地方,以便我可以将这些值推送到模型,从而推送到 MySql 数据库中。我也无法跟踪 Controller 正在渲染什么输出。是否需要通过循环将值推送到数据库中?另请帮助我如何继续将这些数据发送到模型

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

最佳答案

您的 Controller 无法读取发布数据,因为您的表单未使用将值发布到其方法属性的表单标记进行包装。尝试用这个包裹你的表单。

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

您的 Controller 现在应该是

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

如果你想为每个记录执行插入,你的 Controller 应该是

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

关于php - 如何通过codeigniter在mysql中插入多选下拉值( View -> Controller ->模型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53144848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com