douxu3732 2014-12-03 04:57
浏览 63
已采纳

使用Ajax和jQuery - codeigniter提交将数据提交到数据库

I am creating a codeigniter project where i get number of items from my database and each item has a submit button clicking on which the data are stored into the database using AJAX and jQuery. Now, the problem is that when i submit any item only the data of 1st form is sent to the method that inserts data to the database please provide some solution regarding the same. my code is as under:

Model:

<?php
class Products_model extends CI_Model{

    function get_all(){

        $results = $this->db->get('tblUtensils')->result();

        return $results;
    }
    function get($id){
        $results = $this->db->get_where('tblUtensils', array('pkItemID' => $id))->result();
        $result = $results[0];

        return $result;
    }
}


?>

View:

<body>

    <div id="products">
        <?php foreach ($products as $product): 
        $id=$product->pkItemID;
        ?>
            <li>
                <?php echo form_open('shop/add', 'id="form"'); ?>


                    <div><?php echo form_input('name_' . $id, $product->fldName , 'id='.'"'.'name_' . $id.'"');?></div>
                    <div><?php echo form_input('desc_' . $id, $product->fldDescription , 'id='.'"'.'desc_' . $id.'"');?></div>
                    <div><?php echo form_input('rate_' . $id, $product->fldRateMB1 , 'id='.'"'.'rate_' . $id.'"');?></div>
                    <div><?php echo form_input('Qty_' . $id, 0, 'id='.'"'.'qty_' . $id.'"');?></div>
                    <?php echo form_hidden('id', $product->pkItemID, 'id="ID"'); ?>

                    <?php echo form_submit('action', 'Add','id="submit"');?>

                <?php echo form_close(); ?>
            </li>

        <?php endforeach; ?>
    </ul>

    </div>

    <!-- try 2 -->
    <script type = "text/javascript">
$(function(){ 
  $('form').submit(function(e){
    e.preventDefault(); // <------this will restrict the page refresh
     var form_data = {
        id: $('#id').val(),
        name: $('#name_' + id).val(),
        rate: $('#rate_' + id).val(),
        qty: $('#qty_' + id).val()
    };

    $.ajax({
        url: "<?php echo site_url('shop/add'); ?>",
        type: 'POST',
        data: form_data,
        success: function(msg) {
              alert("success");
        }

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

</body>

</html>

Controller:

<?php 

class Shop extends CI_Controller{

    function index(){

        $this->load->model('Products_model');
        $data['products'] = $this->Products_model->get_all();
        $this->load->view('products',$data);


    }

    function add(){
        //$this->load->model('Products_model');
        //$product = $this->Products_model->get($this->input->post('id'));

        $insert = array(
        'id' => $this->input->post('id'),//<-- also not able to get this id from ajax post request
        'qty' => $this->input->post('qty'),
        'price' => $this->input->post('rate'),
        'name' => $this->input->post('name'),
        );
        $this->cart->insert($insert);//<--insert item into cart
        $query// to insert into database

        //redirect('shop');
        }

 }
?>

Many questions have been posted before regarding codeigniter and ajax but none did provide solution to my problem so i am posting this question, appreciate your help.

  • 写回答

2条回答 默认 最新

  • dsjzmrz78089 2014-12-03 17:33
    关注

    I tried many ways of dealing with this and finally the thing that worked for me was as under:

    <script type = "text/javascript">
        $('.submit').click(function(){
            var id = (this.id);
            var form_data = {            //repair
                id: id,
                name: $('#name_' + id).val(),
                rate: $('#rate_' + id).val(),
                qty: $('#qty_' + id).val()
            };
    
        $.ajax({
            url: "<?php echo site_url('shop/add'); ?>",//repair
            type: 'POST',
            data: form_data, // $(this).serialize(); you can use this too
            success: function(msg) {
                  alert("success..!! or any stupid msg");
            }
    
       });
       return false;
    
    });
    
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)