SoftwareDev
SoftwareDev

Reputation: 356

not able to pass data to controller using Ajax with jquery ,CodeIgniter (PHP framework)

passing data to controller using AJAX this is script i have written to pass data to controller but data is not passed to the controller

this is the input data i want to pass

    <div class="form-group">
        <table class="table table-striped b-t b-light text-sm">
            <thead>
                <tr>
                <th>ID</th>
                <th>Question</th>
                <th>answer</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($quet as $row) { ?>
                <tr>
                <td ><?php echo $row['id']; ?></td>
                <td>
                <?php echo $row['question']; ?>
                </td>
                <td><input type='text' name='name' required="required" class="form-control" placeholder='Enter Your Answer'></td>
                </tr>
                <?php } ?>  
            </tbody>
        </table>
    </div>
<button class="btn btn-primary nextBtn btn-lg pull-right" id ="next" type="button" >Next</button>

and the script

<script>
           $(document).ready(function($){
    $("#next").click(function(){

     var array = $("name").val()

     $.ajax({
          type: "POST",
          datatype:"json",
          url: BASE_URL+"/student/add",
          data: 'data='+array,
          contentType:'application/json',
          processData: false,              
          error: function(response) {console.log('ERROR '+Object.keys(response)); },
          success: function(response) {
              console.log(response)

          }});

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

and the student controller

function add(){

      if($this->student_model->add($this->input->post()))
        {
        $response['success'] = TRUE;
        }
        else
        {
        $response['success'] = FALSE;
        }
        echo json_encode($response);  
    }

Upvotes: 0

Views: 836

Answers (4)

Kiren S
Kiren S

Reputation: 3097

Try this. Your data may be in wrong format

data: {'data':array}

EDIT

 <input type='text' name='answer' id='answer' required="required" class="form-control" placeholder='Enter Your Answer' />
<script>
   $(document).ready(function($){
        $("#next").click(function(){

        var array = $("#answer").val() // see the change name to id, see the html also

         $.ajax({
             type: "POST",
              url: BASE_URL+"/student/add",
              data:{'data':array},
              error: function(response) {console.log('ERROR '+Object.keys(response)); },
              success: function(response) {
                 console.log(response)
              }});
         });  
    });
 </script>

Upvotes: 1

Hardik Ranpariya
Hardik Ranpariya

Reputation: 1051

You need to change following in your code.

In the Script

<script>
$(document).ready(function($){
    $("#next").click(function(){

     var array = $("input[name]").val();

     $.ajax({

          type: "POST",
          url: BASE_URL+"/student/add",
          data: {'data':array},            
          error: function(response) {console.log('ERROR '+Object.keys(response)); },
          success: function(response) {
              console.log(response)

          },
          datatype:"json"
     });

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

In the Student Controller

function add(){

      if($this->student_model->add($this->input->post('data')))
        {
            $response['success'] = TRUE;
        }
        else
        {
            $response['success'] = FALSE;
        }
        echo json_encode($response);  
    }

I hope this help.

Upvotes: 0

Jenis Patel
Jenis Patel

Reputation: 1613

From your mentioned code, you need to check these points :

1) As you are using contentType:'application/json', so use the data format as data: {'data':array}

2) Finally check whether the url url: BASE_URL+"/student/add", is accessible or not.

Hope this helps :)

Upvotes: 0

Hoja
Hoja

Reputation: 1207

Also check the array is received properly in you JS

<script>
  $(document).ready(function($){
    $("#next").click(function(){

     var array = $("#id").val()

     $.ajax({
          type: "POST",
          datatype:"json",
          url: BASE_URL+"/student/add",
          data: {
            'data':array
          },
          contentType:'application/json',
          processData: false,              
          error: function(response) {console.log('ERROR '+Object.keys(response)); },
          success: function(response) {
              console.log(response)

          }});

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

In your controller you should change $this->input->post() to$this->input->post('data')

Upvotes: 0

Related Questions