0

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);  
    }
6
  • first of all you've to change the name of the input textbox from 'name'. Then you've to assign an id or a unique class to it. after that you could call the jquery as var array = $("#id").val() Commented Jan 7, 2015 at 8:27
  • @Hoja.M.A i have added code for input text Commented Jan 7, 2015 at 9:01
  • where is the id="next" button, it's in your code or not..? Commented Jan 7, 2015 at 9:03
  • @HardikRanpariya yes id ="next" button it is in code i have edit the question Commented Jan 7, 2015 at 9:08
  • okey, then check my code again, i edit that code. Commented Jan 7, 2015 at 9:09

4 Answers 4

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget to check the browser utility to monitor the ajax request when u click on the next button
0

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')

Comments

0

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 :)

4 Comments

var array = $("name").val() what is the relation ship with class here? If there is a class "name" then it must be referred like $(".name").val() (dot infront)
@Kiren, yes you are right, I did not notice it, I thought it is $(".name").val() but it was $("name").val().
@jenis input text is contain in this div <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>
@SoftwareDev, Just try and make use of Firebug (Mozilla addon) Console so that you can debug and figure out the root cause. And just try this ajax url value => url: BASE_URL+"/index.php/student/add",
0

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.

4 Comments

var array = $("#name").val() for this he has to add id attribute in his input element?
@Hardik Ranpariya $("#name").val() so i have to assign id to input field id="name"
i already change this. you can now not need to assign id to input field id="name"
@Hardik Ranpariya well its not showing any error but data is not submitted

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.