[英]Inserting and Retrieving array data using ajax in codeigniter
我试图在Codeigniter中使用Ajax插入和检索数组数据,我尝试了以下代码不起作用,任何人都可以告诉我解决方案。
$('#click').click(function(){
var selected = new Array();
$(".stafftable input[type=checkbox]:checked").each(function(){
selected.push(this.value);
});
var selected_member_id=localStorage.getItem('selectids');
var myarray=selected_member_id.split(','); // convert string to an array
$.ajax({
type: "POST",
url:"<?php echo base_url();?>Welcome/send_to_staff",
data: {staff_id:selected,members_id:myarray},
success:
function(data)
{
if(data==true){
// localStorage.clear();
load_unseen_notification();
}
else{
alert("localstorage not cleared");
}
}
});
});
在Controller中,但我尝试了以下代码,但未插入数据库表中
public function send_to_staff(){
$staff_id=$this->input->post('staff_id');
$membersids[]= $this->input->post('members_id');
$members_id=json_encode($membersids);
if($staff_id !==''){
$data['staff_id']=$staff_id;
$data['members_id']=$members_id;
$inserted=$this->db->insert('ag_matched_members',$data); // insert query
if($inserted == true){
echo true;
}
else
{
echo false;
}
}
}
您必须在代码中进行类似的更改。 希望它能工作。
$('#click').click(function(){
var selected = new Array(); //assigning array to variable
$(".stafftable input[type=checkbox]:checked").each(function(){
selected.push(this.value);
});
var selected_member_id=localStorage.getItem('selectids');
var string_selected=selected.toString(); //converting array to string
$.ajax({
type: "POST",
url:"<?php echo base_url();?>Welcome/send_to_staff",
data: {staff_id:string_selected,members_id:selected_member_id}, //string selected
success:
function(data)
{
if(data==true){
// localStorage.clear();
load_unseen_notification();
}
else{
alert("localstorage not cleared");
}
}
});
});
在控制器中像这样尝试,它可能会起作用。 它对我有用
public function send_to_staff(){
$staff_id=$this->input->post('staff_id');
$staff_id=json_encode($staff_id); //converting to json
$membersids[]= $this->input->post('members_id');
$members_id=json_encode($membersids); //converting to json
if($staff_id !==''){
$data['staff_id']=$staff_id;
$data['members_id']=$members_id;
$inserted=$this->db->insert('ag_matched_members',$data); //insert query
if($inserted == true){
echo true; //returns true
}
else
{
echo false; //returns false
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.