2017-04-05 41 views
0

我试图发布多个值的选择元素到PHP,这只是在.ajax发布错误。它适用于HTML表单发布。无法发布选择多个使用jQuery .ajax()到php codeIgniter

JS:

function serealizeSelects(select){ 
     var array = []; 
     select.each(function(){ array.push($(this).val()) }); 
     return array; 
    } 

$("#go").on('click', function(e){ 
    e.preventDefault(); 
    if($("#title").val()=='') swal('','Please enter the title.','warning'); 
    else{ 
     $("#go").text('Publishing...'); 
     $("#go").prop('disabled',true); 
     $.ajax({ 
      url: '<?=base_url();?>'+'classes/add_notes', 
      type: "POST", 
      data: { 
      title: $("#title").val(), 
      content: $("#content").val(), 
      file: upFile, 
      class: JSON.stringify(serealizeSelects($('.selectpicker'))), 
      <?=$this->security->get_csrf_token_name();?>:'<?=$this->security->get_csrf_hash();?>', 
      }, 
      dataType: "json", 
      success: function(data){ 
      $("#go").text('Publish'); 
      $("#go").prop('disabled',false); 
      if(data.error=='false'){ 
       swal('Published','Your notes has been shared to selected classes.'); 
      } 
      else{ 
       if(data.error_code=='1') swal('','Please fill fields properly!','warning'); 
       else if(data.error_code=='2') swal('','Unauthorised','error'); 
       else swal('','Something went wrong','error'); 
      } 
      }, 
      error: function(){ 
      $("#go").text('Publish'); 
      $("#go").prop('disabled',false); 
      swal('','Some error occured','error'); 
      } 
     }); 
     } 
    }); 

HTML:

<select class="selectpicker" data-live-search="true" id="class" multiple> 
     <option value="0">Choose classes...</option> 
       <?=$classes;?> 
</select> 

** PHP(笨):**

$this->form_validation->set_rules('title', 'title', 'trim|required|xss_clean'); 
     $this->form_validation->set_rules('content', 'content', 'trim|xss_clean'); 
     $this->form_validation->set_rules('file', 'file', 'trim|xss_clean'); 
     $this->form_validation->set_rules('class[]', 'class', 'trim|required|xss_clean'); 

     if($this->form_validation->run() == FALSE OR $this->end!='teacher'){ 
     $response['error']="true"; 
     $response['error_code']="1"; 
     } 
     else{ 
      $title=$this->input->post('title'); 
      $content=$this->input->post('content'); 
      $file=$this->input->post('file'); 
      $classes=json_decode($this->input->post('class[]'),true); 
      foreach($classes as $class){ 
       // Some task 
      } 
     } 

我已经找了很多解决方案,没有一个工作。 我也用也试过,而不是class []。 没有工作! 发布此执行错误 ajax的一部分。

+1

是什么错误信息在控制台说HTML元素的价值? –

+0

不,它只是执行错误funcn。 ajax –

+0

我在postman中调试过,在php中收到的数组为null –

回答

0

待办事项以下变化:

<select class="selectpicker" data-live-search="true" id="class" multiple> 

<select name="selectpicker[]" class="selectpicker" data-live-search="true" id="class" multiple> 

,并得到其价值,如:

$this->input->post('selectpicker'); 

说明:对于multi-select dropdown,它的名字必须和数组一样​​,以便它可以举行在其中选择多个值并通过在php中使用其名称来获取其值。

你永远不能在PHP中使用它的类直接你尝试过$this->input->post('class[]'),true);

+0

“它的名字必须是一个类似于'selectpicker []''的数组” - 然后,不应该使用'name =“selectpicker []”'而不是'class =“selectpicker []”'? ;) – roberto06

+0

对不起,只是忘记了:P –

+0

更换后仍然有同样的错误。虽然php代码在html表单发布后工作正常。 有没有任何问题。在js的序列化函数中。 –