jquerysqlajaxcodeigniterformwizard

To pass input data as array to jquery


i have the requirement where student answer questions (which questions retrive from database) using foreach,and i have to pass to jquery when all answer filled and then to controller and save to database.

<div>
    <tbody>
        <?php foreach ($questions as $question) { ?>
        <tr>
            <td ><?php echo $question['id']; ?></td>
            <td> <?php echo $question['question']; ?> </td>
            <td><input type='text' name='name' required="required" class="form-control" placeholder='Enter Your Answer'></td>
            // this is answer field associated with each question
        </tr>
        <?php } ?>  
    </tbody> 
</div>
<button class="btn btn-primary nextBtn btn-lg pull-right" id ="next" type="button" >Next</button>

and when student fills all answer this (question ans answer) pass to jquery and this is script

<script>
          $(document).ready(function($){
            $(".gg").click(function(){
            var array = $("input[name]").val();
            $.ajax({
          type: "POST",
          datatype:"json",
          url: BASE_URL+"student/info",
          data: 'data='+array,

          success: function(response) {


          }});

        return false;
    });  
});
        </script>    

when student submit answer, only first answer is pass to jquery function ,not able to pass all answer


Solution

  • data is not proper fomat

    data: {'data=': array_variable },
    

    If you want to send all form data better you could serialize it and send it to your php side and serialize there.Like:

    data : $('#form').serialize()
    

    ELSE Without Serialize

    var textboxes = $('input[name="answer[]"]').val();
    data : {'data':textboxes},