【问题标题】:Submit data to database using Ajax and jQuery - codeigniter submission使用 Ajax 和 jQuery 向数据库提交数据 - codeigniter 提交
【发布时间】:2014-12-03 04:57:20
【问题描述】:

我正在创建一个 codeigniter 项目,我从我的数据库中获取项目数量,每个项目都有一个提交按钮,单击该按钮将使用 AJAX 和 jQuery 将数据存储到数据库中。 现在,问题是当我提交任何项目时,只有第一种形式的数据被发送到将数据插入数据库的方法,请提供一些解决方案。我的代码如下:

型号:

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


?>

查看:

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

控制器:

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

 }
?>

之前已经发布了许多关于 codeigniter 和 ajax 的问题,但没有一个可以解决我的问题,所以我发布了这个问题,感谢您的帮助。

【问题讨论】:

  • 请在此处阅读有关 DOM 的信息:kangax.github.io/domlint 您的表单具有相同的 ID。
  • 那么解决办法是什么?

标签: php jquery ajax database codeigniter


【解决方案1】:

改变

<?php echo form_open('shop/add', 'id="form"'); ?>

<?php echo form_open('shop/add', 'id="form_' . $id . '"'); ?>

这为每个表单提供了不同的 ID。

【讨论】:

    【解决方案2】:

    我尝试了很多方法来解决这个问题,最后对我有用的是:

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

    【讨论】:

    • 我将 class="submit" 和 id="id" 添加到我的提交按钮,所以现在我可以使用它的 onClick 事件来获取 id,这反过来是我需要分配给我的值变量“id”..也感谢大家的帮助..
    猜你喜欢
    • 2017-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多