Aslam Kakkove
Aslam Kakkove

Reputation: 97

CodeIgniter Json Ajax Database insertion is not working

I have tried to solve this issue, here i am inserting dynamic inputbox box values into database including their title. But not working...

Inputbox dynamic generation: (This works well)

 $('#myTable tbody').append("<tr><td>"+rno+"</td><td>"
            +item.stdname+"</td><td><input type='text' name='stdmark[]' class='mark' title='"+item.stdid+"' style='padding: 0px; width: 50px;'/></td></tr>");

Ajax to Send these values into controller:

$('#marklist').submit(function(e){
  //var mark = 10;

    jsonObj = [];
    $("input[class=mark]").each(function() {

        var id = $(this).attr("title");
        var subjectmark = $(this).val();

        item = {}
        item ["stdid"] = id;
        item ["mark"] = subjectmark;

        jsonObj.push(item);
    });

  $.ajax({
      type: "POST",
      url: "<?php echo base_url(); ?>office/addmark",
      data: {senddata :JSON.stringify(jsonObj)},
      dataType: "json",
      processData:false,
      contentType:false,
      cache:false,
      async:false,
      success:
           function(retrived_data){


                }
       });
e.preventDefault();
});

Controller:

public function addmark()
{
 $marks = json_decode($this->input->post('senddata'), true);
  $this->load->Model('wtcmodel');
foreach($marks as $row)
      {
        $data = array(
          'stdid' =>  $row->stdid,
          'mark' => $row->mark
        );
      $this->wtcmodel->adddata($data);
    }
}

Model:

public function adddata($data)
         {
              $this->load->database();
              $this->db->insert('table_info',$data);
          }

Upvotes: 0

Views: 196

Answers (2)

Mayank Majithia
Mayank Majithia

Reputation: 1966

you can post input data using this method.

 var stdid = $('input[name="stdmark[]"]').map(function(){ 
                    return $(this).attr('title');
                }).get();

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

 $.ajax({
    type: 'POST',
    url: 'users.php',
    data: {
        'stdid[]': stdid,
        'marks[]':marks
    },
    success: function() {
    }
});

You can access stdid[] and marks[] variable as array directly in controller.

Controller

public function addmark()
{
 $stdid = $this->input->post('stdid');
 $marks = $this->input->post('marks');
 $this->load->Model('wtcmodel');
 foreach($stdid as $key => $row)
      {
        $data = array(
          'stdid' => $stdid,
          'mark' => $marks[$key]
        );
      $this->wtcmodel->adddata($data);
    }
}

Upvotes: 1

mohamedvall
mohamedvall

Reputation: 144

why did't use Jquery serialize function : http://api.jquery.com/serialize/

 $('#marklist').submit(function(e){
      e.preventDefault();
      //var mark = 10;

      $.ajax({
          type: "POST",
          url: "<?php echo base_url(); ?>office/addmark",
          data: $(this).serialize(),
          dataType: "json",
          processData:false,
          contentType:false,
          cache:false,
          async:false,
          success:
          function(retrived_data){}
       });


  });

or you can use param : http://api.jquery.com/jquery.param/

Upvotes: 0

Related Questions