在codeigniter中使用ajax插入和检索数组数据

我正在尝试在 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");

                          }


                      }

            });

    });

在控制器中,但我尝试了下面的代码,但没有插入数据库表


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;

                }


            }


}


qq_遁去的一_1
浏览 120回答 1
1回答

波斯汪

您必须在代码中进行这样的更改。希望它有效。$('#click').click(function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var selected = new Array(); //assigning array to variable&nbsp; &nbsp; &nbsp; &nbsp; $(".stafftable input[type=checkbox]:checked").each(function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selected.push(this.value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp;var selected_member_id=localStorage.getItem('selectids');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var string_selected=selected.toString(); //converting array to string&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: "POST",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url:"<?php echo base_url();?>Welcome/send_to_staff",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: {staff_id:string_selected,members_id:selected_member_id}, //string selected&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function(data)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(data==true){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // localStorage.clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; load_unseen_notification();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("localstorage not cleared");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; });在控制器中尝试这样,它可能会起作用。它对我有用public function send_to_staff(){&nbsp; &nbsp; $staff_id=$this->input->post('staff_id');&nbsp; &nbsp; $staff_id=json_encode($staff_id); //converting to json&nbsp; &nbsp; $membersids[]= $this->input->post('members_id');&nbsp; &nbsp; $members_id=json_encode($membersids);&nbsp; //converting to json&nbsp; &nbsp; if($staff_id !==''){&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $data['staff_id']=$staff_id;&nbsp; &nbsp; &nbsp; &nbsp; $data['members_id']=$members_id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$inserted=$this->db->insert('ag_matched_members',$data); //insert query&nbsp; &nbsp; &nbsp; &nbsp; if($inserted == true){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo true; //returns true&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo false;&nbsp; //returns false&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP