0

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);
          }

2 Answers 2

1

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

6 Comments

I want to post input values and their title to the controller
but.. not working.. there was a hidden input value at first, i removed it. Actually i want to insert mark of a student with id. I made the id as the input box 'title'. then i used json
Then u have to iterate stdmark[] twice .. I had update answer
Even this dummy values are not working. $stdid = $this->input->post('stdid'); $marks = $this->input->post('mark'); $this->load->Model('wtcmodel'); foreach($stdid as $key => $row) { $data = array( 'stdid' => 10, 'mark' => 25 ); $this->wtcmodel->addmarks($data); }
Then there is some problem with db configuration. Please check there also
|
0

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/

1 Comment

I want to post input values and their title to the controlle

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.