CodeIgniter Json Ajax数据库插入不起作用

问题描述 投票:0回答:2

我试图解决这个问题,这里我将动态输入框框值插入数据库,包括他们的标题。但不工作......

输入框动态生成:(效果很好)

 $('#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将这些值发送到控制器:

$('#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();
});

控制器:

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

模型:

public function adddata($data)
         {
              $this->load->database();
              $this->db->insert('table_info',$data);
          }
php json ajax codeigniter model-view-controller
2个回答
© www.soinside.com 2019 - 2024. All rights reserved.