[英]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.