0

I have the following problem, I need to save the values of a checkbox group and send them as array by AJAX to php, so far I have the following code:

 
<form id="formid">
  <input type="checkbox" value="1" name="page[]" class="up">
  <input type="checkbox" value="2" name="page[]" class="up">
  <input type="checkbox" value="3" name="page[]" class="up">
  <input type="checkbox" value="4" name="page[]" class="up">
  <input type="checkbox" value="5" name="page[]" class="up">
  <a href="#" id="enviar" />create</a>
</form>

<script type="text/javascript">
       $(document).ready(function() {
        $('#send').click(function() {
         $.ajax({
        type: "POST",
        dataType: 'json',
        data: { 
                'ids': JSON.stringify($('[name="page[]"]').serializeArray())
              },
        url: "<?php echo site_url();?>/rols/pages",
        success : function(data) {
           
         }
          });
        });
      });
    </script>

I need to saver how to process that array in my php model, I'm using codeigniter, and I need to walk through each of the values obtained from my ajax, and be able to save them in my database.

Please any input would be greatly appreciated

  • 2
    What is not working ? – Mihai Alexandru-Ionut Jan 26 '17 at 14:03
  • `serializeArray()` is intended for use on `form` elements so isn't giving the output you expect. You can instead use `map()` to build the array you require. See the duplicate question for details on exactly how to do that. I'd also suggest that you use a submit button in the form, and hook to the `submit` event of the form. – Rory McCrossan Jan 26 '17 at 14:06
  • I have just seen the question you suggest, and I do not understand the change I must make, you also say that I change my form to the post method, not to execute my ajax within the event click of my button – Fernando Banegas Jan 26 '17 at 14:19

0 Answers0