在动态输入中将数据插入到codeigniter中的数据库中

时间:2017-05-27 23:49:20

标签: codeigniter

大家。

我是CodeIgniter的初学者,我试图将数据从动态输入插入数据库,我搜索得太多但是到目前为止我什么也没得到

到目前为止我的模型是空的,因为我不知道如何从动态输入中插入数据

这是我的观点

  <form id="bookForm" method="post" class="form-horizontal"action="order/insert">
            <div class="form-group">
                <div class="col-xs-3">
                <label for="customer_name">Customer :
                    <br>
                <select class="selectpicker" data-show-subtext="true" data-live-search="true" name="customer_name" id="customer_name">
                   <?php foreach ($customerdata as $c):
                    echo "<option  value ='$c->c_id'>" . $c->c_name . "</option>";
                    endforeach;
                    ?>
                </select>
                </label>
                </div>
                <div class="col-xs-3">
                    <label for="invoice_number">Invoice Number :
                <input type="text" class="form-control" name="invoice_number" id="invoice_number" placeholder="Invoice Number"/>
                    </label>
                </div>
                <div class="col-xs-3">
                    <label for="branch">Branch :
                        <input type="text" class="form-control" name="branch" id="branch" placeholder="Branch"/>
                    </label>
                </div>
                <div class="col-xs-3">
                    <label for="payment_term">Payment Term :
                        <br>
                        <select class="selectpicker" data-show-subtext="true" data-live-search="true" name="payment_term" id="payment_term">
                            <option id-="cash">Cash</option>
                            <option id-="bank">Bank</option>
                            <option id-="other">Other</option>
                        </select>
                    </label>
                </div>
                <br>

                <br><br><br><hr>
                <label class="col-xs-1 control-label">Product</label>

                <div class="col-xs-4">
                    <select class="selectpicker" data-show-subtext="true" data-live-search="true" name="product[0].title">
                        <?php
                        foreach($order as $row):
                            echo"<option value ='$row->p_id'>".$row->p_name. "</option>";
                        endforeach;
                        ?>
                    </select>
                </div>
                <div class="col-xs-4">
                    <input type="text" class="form-control" name="product[0].qty" placeholder="Quantity" />
                </div>
                <div class="col-xs-2">
                    <input type="text" class="form-control" name="product[0].price" placeholder="Price" />
                </div>
                <div class="col-xs-1">
                    <button type="button" class="btn btn-default addButton"><i class="fa fa-plus"></i></button>
                </div>
            </div>

            <!-- The template for adding new field -->
            <div class="form-group hide" id="bookTemplate">
                <div class="col-xs-4 col-xs-offset-1">
                    <select class="selectpicker" data-show-subtext="true" data-live-search="true" name="title">
                        <?php
                        foreach($order as $row):
                            echo"<option value ='$row->p_id'>".$row->p_name. "</option>";
                        endforeach;
                        ?>
                    </select>
                </div>
                <div class="col-xs-4">
                    <input type="text" class="form-control" name="qty" placeholder="Quantity" />
                </div>
                <div class="col-xs-2">
                    <input type="text" class="form-control" name="price" placeholder="Price" />
                </div>
                <div class="col-xs-1">
                    <button type="button" class="btn btn-default removeButton"><i class="fa fa-minus"></i></button>
                </div>
            </div>

            <div class="form-group">
                <div class="col-xs-5 col-xs-offset-1">
                    <button type="submit" class="btn btn-default">Submit</button>
                </div>
            </div>
        </form>

这个Jquery

<script>
$(document).ready(function() {
    var titleValidators = {
            row: '.col-xs-4',   // The title is placed inside a <div class="col-xs-4"> element
            validators: {
                notEmpty: {
                    message: 'Product is required'
                }
            }
        },
        isbnValidators = {
            row: '.col-xs-4',
            validators: {
                notEmpty: {
                    message: 'Quantity is required'
                }
            }
        },
        priceValidators = {
            row: '.col-xs-2',
            validators: {
                notEmpty: {
                    message: 'Invoice Number is required'
                },
                numeric: {
                    message: 'The invoice number  must be a numeric number'
                }
            }
        },
        bookIndex = 0;

    $('#bookForm')
        .formValidation({
            framework: 'bootstrap',
            icon: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                'product[0].title': titleValidators,
                'product[0].qty': isbnValidators,
                'product[0].price': priceValidators
            }
        })

        // Add button click handler
        .on('click', '.addButton', function() {
            bookIndex++;
            var $template = $('#bookTemplate'),
                $clone    = $template
                    .clone()
                    .removeClass('hide')
                    .removeAttr('id')
                    .attr('data-book-index', bookIndex)
                    .insertBefore($template);

            // Update the name attributes
            $clone
                .find('[name="title"]').attr('name', 'product[' + bookIndex + '].title').end()
                .find('[name="qty"]').attr('name', 'product[' + bookIndex + '].isbn').end()
                .find('[name="price"]').attr('name', 'product[' + bookIndex + '].price').end();

            // Add new fields
            // Note that we also pass the validator rules for new field as the third parameter
            $('#bookForm')
                .formValidation('addField', 'product[' + bookIndex + '].title', titleValidators)
                .formValidation('addField', 'product[' + bookIndex + '].qty', isbnValidators)
                .formValidation('addField', 'product[' + bookIndex + '].price', priceValidators);
        })

        // Remove button click handler
        .on('click', '.removeButton', function() {
            var $row  = $(this).parents('.form-group'),
                index = $row.attr('data-book-index');

            // Remove fields
            $('#bookForm')
                .formValidation('removeField', $row.find('[name="product[' + index + '].title"]'))
                .formValidation('removeField', $row.find('[name="product[' + index + '].qty"]'))
                .formValidation('removeField', $row.find('[name="product[' + index + '].price"]'));

            // Remove element containing the fields
            $row.remove();
        });
});

这是我的控制器

function OrderNow(){

$this->load->model('Out_m');
$this->Out_m->outs();
$this->session->set_flashdata('done','Your Order Submitted Successfully');
redirect('Order', 'refresh');

}

1 个答案:

答案 0 :(得分:0)

如果您认为有必要,只需验证您的表单输入:

$this->load->library('form_validation');
$this->form_validation->set_rules('branch', 'Branch', 'required|trim');

确保您的表单验证不会失败。

if ($this->form_validation->run() === FALSE) {
    // Throw some error or whatever
} else {
    $form_data = array('branch'=>set_value('branch');
}

然后把它扔到模特身上:

$this->Out_m->handling_function($form_data);

一旦你到达那里,你就可以随心所欲地做任何事情。