title: How can use ajax-jquery to get selected checkbox data to modal using PHP-CodeIgniter
body: <p>how to use an HTML table with the checkbox in all rows and when clicking on a button I want to get those selected checkbox IDs to a modal. so I want to use ajax for getting related data of selected IDs from the database and show in a table in modal using PHP-CodeIgniter. anybody could help me to get this? </p>
  
  <p>I have tried with a lot of ways but all were a failure.</p>
  
  <p>Thanks a lot in advance.</p>
  
  <p>Please see my code.</p>
  
  <p>On my VIEW page table</p>
  
  <pre><code>&lt;table id="dynamic-table"&gt;
          &lt;thead&gt;
              &lt;th&gt; Check&lt;/th&gt;
              &lt;th&gt; Name&lt;/th&gt;
              &lt;th&gt; Country&lt;/th&gt;
          &lt;/thead&gt;
          &lt;tbody&gt;
              &lt;tr&gt;
                  &lt;td&gt; &lt;input type="checkbox" name="ch1" value="1"&gt; &lt;/td&gt;
                  &lt;td&gt; Rashid &lt;/td&gt;
                  &lt;td&gt; India &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt; &lt;input type="checkbox" name="ch1" value="2"&gt; &lt;/td&gt;
                  &lt;td&gt; Nishad &lt;/td&gt;
                  &lt;td&gt; India &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt; &lt;input type="checkbox" name="ch1" value="3"&gt; &lt;/td&gt;
                  &lt;td&gt; sajeesh &lt;/td&gt;
                  &lt;td&gt; India &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/tbody&gt;
  
      &lt;/table&gt;
  
      &lt;button name="submit" id="subm"&gt; Submit &lt;/button&gt;
      &lt;button name="cancel" id="canc"&gt; Cancel &lt;/button&gt;
  
  
      &lt;div id="message"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>Scrip on VIEW Page:</p>
  
  <pre><code>&lt;script&gt;
      $(document).ready(function(){
          $("#subm").click(function(){
              getValueUsingParentTag();
          });
      });
      function getValueUsingParentTag(){
  var chkArray = [];
  
  /* look for all checkboes that have a parent id called 'checkboxlist' attached to it and check if it was checked */
  $("#dynamic-table input:checked").each(function() {
      chkArray.push($(this).val());
  });
  
  /* we join the array separated by the comma */
  var selected;
  selected = chkArray.join(',') ;
  
  
  
  
  var url = "&lt;?php echo base_url(); ?&gt;/index.php/Ajax";
      $.post( url, { test_input: selected } ) 
  
           .done(function(data){
              $("#message").html(data.result);
          });
  
  
  
  
  
  /* check if there is selected checkboxes, by default the length is 1 as it contains one single comma */
  if(selected.length &gt; 0){
      alert("You have select" + selected);    
  }else{
      alert("Please at least check one of the checkbox"); 
  }
  </code></pre>
  
  <p>}
      </p>
  
  <p>My AJAX Controller</p>
  
  <pre><code>&lt;?php
  </code></pre>
  
  <p>defined('BASEPATH') OR exit('No direct script access allowed');</p>
  
  <p>class Ajax extends CI_Controller {</p>
  
  <pre><code>public function index()
  {
      $posted = $this-&gt;input-&gt;post('test_input');
      echo json_encode(array('result'=&gt; $posted));        
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Please suggest me the edits or a good one. Actually, I want to retrieve data from DB. But in this code first, I just tried to work with ajax without db. once this got success then I can go ahead with DB data retrieve.</p>