PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, September 10, 2022

[FIXED] How do I post the data from select2 plugin using ajax?

 September 10, 2022     ajax, codeigniter-3, jquery     No comments   

Issue

I try to post the data from select2 but it's not working at all any one have any idea?

By the way I'm using the code igniter 3 for my framework.

My code

$('#reference').change(function(){
   var ref = $(this).val();
   $.ajax({
      type: 'POST',
      url: '<?= base_url('/user/getItem')?>',
      data: {
         ref: ref
      },
      dataType: 'JSON',
      success: function(data){
            console.log('anything');
         }
      })
});

html

<select multiple="multiple" id="reference" name="reference[]">
   <?php foreach($ref as $r):>
      <option value="<?= $r['id']?>"><?= $r['name']?></option>
   <?php endforeach;>
</select>

I also tried this code but also not work

 $('#reference').select2({
            closeOnSelect: true,
            placeholder: "Select Reference",
            
            ajax: {
                type: 'POST',
                url: '<?= base_url('/user/getItem')?>',
                data: {
                    ref: $(this).val()
                },
                dataType: 'JSON',
                success: function(data){
                    console.log('anything');
                }
            }
        });

Solution

Try to change "type" to "method"

$('#reference').change(function(){
  var ref = $(this).val();
  $.ajax({
      method: 'POST',
      url: '<?= base_url('/user/getItem')?>',
      data: {
         ref: ref
      },
      dataType: 'JSON',
      success: function(data){
          console.log('anything');
      }
  })
});


Answered By - Game Wapps
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing