0

I'm trying to post multiple values of select element to php, which is giving error only in .ajax post. It works fine with HTML form post.

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 (CodeIgniter): **

$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
            }
      }

I've looked for lots of solution, none of which worked. I tried with class also, instead of class[]. Nothing worked! Posting this executes error part of ajax.

3
  • 1
    What does error message say in console ? Commented Apr 5, 2017 at 8:09
  • No, It just executes error funcn. of ajax Commented Apr 5, 2017 at 11:34
  • I debugged in postman, array received at php is null Commented Apr 5, 2017 at 16:49

1 Answer 1

0

Do following changes:

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

to

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

and get its value like:

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

Explanation: For multi-select dropdown, its name must be and array like selectpicker[] so that it can hold multiple selected value in it and get its value by using its name in php.

You can never get a html element value in php using its class directly as you tried $this->input->post('class[]'),true);

Sign up to request clarification or add additional context in comments.

4 Comments

" its name must be an array like selectpicker[]" - Then, shouldn't you use name="selectpicker[]" instead of class="selectpicker[]" ? ;)
Still same error after changing. Though the php code is working fine with html form post. Is there any prob. in serialize function of js.
I debugged the problem. php is receiving null array after decoding
What if I encode the select array to base_64 instead of json_encode

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.