0

Here is my view:

<div class="form-group col-md-3">
    <label class="sup col-md-12 control-label">Employees</label>
        <?php
        if(isset($hiddenEmpArray)){
          if(is_array($hiddenEmpArray)){
            foreach($hiddenEmpArray as $hiddenEmpArraySingle){
echo '<input type="hidden" name="selectall[]" id="selectall" value="'. $hiddenEmpArraySingle. '">';
                }
            }
        }
    ?>
</div>

Javascript:

$('#form').submit(function(e){
          e.preventDefault();

          var selectall                     =$("#selectall").val();


          $.ajax({
                type: "POST",
                url: "<?php echo base_url()?>",
                data: {selectall:selectall},
                success: function (data) {
                    //alert(data);

                },
                error: function () {
                    alert("Server Error! Please try again later.");
                }
            });
      });

Here I want to Submit this form through javascript.Here selectall is an array.When I Submit the form,Only One value is received .How Can I pass this array through javascript.Please help me

5
  • 3
    id is unique and never be duplicate, so retrive all data using name. Commented Nov 26, 2018 at 8:53
  • How?.Please help me Commented Nov 26, 2018 at 8:53
  • $("input[name='selectall[]']").val(); api.jquery.com/attribute-equals-selector Commented Nov 26, 2018 at 8:54
  • I used this script.But receiving only one Value,When I pass More than one value Commented Nov 26, 2018 at 9:01
  • Possible duplicate of Why are duplicate ids not allowed in HTML Commented Nov 26, 2018 at 9:42

3 Answers 3

1

The serialize() method creates a URL encoded text string by serializing form values.

 $('#form').submit(function(e){
              e.preventDefault();
              var formId = $(this).attr('id');//getting form id 

              $.ajax({
                    type: "POST",
                    url: "<?php echo base_url()?>", 
                    data: $('#' + formId).serialize(),//jquery id selector for the form 
                    success: function (data) {
                        //alert(data);

                    },
                    error: function () {
                        alert("Server Error! Please try again later.");
                    }
                });
          });
Sign up to request clarification or add additional context in comments.

2 Comments

There is an error Occured:Uncaught SyntaxError: Unexpected string
@Unknownhacker does this line give you the error?- data: $('#' + formId).serialize() or any other line?
0

you can just use this

var selectall = $("input[name='selectall[]']").map(function(){return $(this).val();}).get();

and then in success just do console.log(data);

Comments

0

you may use jquery each function to collect data

var selectall=[];
$.each($("input[name='selectall[]']"), function(){            
    selectall.push($(this).val());
});

Comments

Your Answer

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