2017-04-05 44 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的序列化函數中。 –