如何让用户在发票中选择一个项目,而不是在codeigniter中再次选择相同的项目

时间:2018-01-05 15:04:59

标签: jquery codeigniter

我正在制作发票页面并且用户从下拉列表菜单中选择他/她的项目,但是用户可以在每个下拉列表中选择相同产品的问题,我想如果他从任何下拉菜单中选择产品都不能再选一次。

enter image description here

这是我的观点

    <?php include_once('header_v.php');?>
    <?php
    include_once ('menu_v.php');
    ?>

    <!-- Page Content -->
    <div id="page-wrapper">
        <div class="container-fluid">

            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-header">Invoice</h1>
                </div>
            </div>
            <?php if($this->session->flashdata('done')): ?>
                <div class="alert alert-success">
                    <strong>Success!</strong> <?php echo $this->session->flashdata('done'); ?>
                </div>
            <?php endif;?>
            <?php if($this->session->flashdata('error')): ?>
                <div class="alert alert-warning">
                    <strong>Warning!</strong> <?php echo $this->session->flashdata('error'); ?>
                </div>
            <?php endif;?>
            <!-- ... Your content goes here ... -->

            <?php $form=array('id'=>'myform');?>
        <?php echo form_open('Order/submit',$form);?>
            <div class="panel panel-default">
                <div class="panel-heading">Customer Details</div>
                <div class="panel-body">
                    <div class="col-xs-3">
                            <select class="selectpicker" data-show-subtext="true" data-live-search="true" name="customer_name">
                                <?php foreach ($customerdata as $c):
                                    echo "<option  value ='$c->c_id'>" . $c->c_name . "</option>";
                                endforeach;
                                ?>
                            </select>
                    </div>
                    <div class="col-xs-3">
                            <input type="text" class="form-control" name="invoice_number"  placeholder="Invoice Number"/>
                    </div>
                    <div class="col-xs-3">
                            <input type="text" class="form-control" name="branch"  placeholder="Branch"/>
                    </div>
                    <div class="col-xs-3">
                            <select class="selectpicker" data-show-subtext="true" data-live-search="true" name="payment_term">
                                <option value="cash">Cash</option>
                                <option value="bank">Bank</option>
                                <option value="other">Other</option>
                            </select>
                    </div>
                </div><!--customer panel-Body-->
                <div class="panel-heading">Invoice Details
                </div>

                <div class="panel-body">

                    <div id="education_fields">


                    <div class="col-sm-3 nopadding">
                        <div class="form-group">
                            <select class="selectpicker" data-show-subtext="true" data-live-search="true" name="select_product[]">
                                <option></option>
                                <?php
                                foreach($order as $row):
                                    echo"<option data-price='$row->p_price' data-qty='$row->s_p_qty' value ='$row->s_p_id'>".$row->s_p_name. "</option>";
                                endforeach;
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class="col-sm-3 nopadding">
                        <div class="form-group">
                            <input type="text" class="form-control qty" onkeypress="return event.charCode >= 48 && event.charCode <= 57;set_qty()" name="qty[]" value="" placeholder="Quantity">
                        </div>
                    </div>
                    <div class="col-sm-3 nopadding">
                        <div class="form-group">
                            <input type="text" class="form-control price"  name="price[]" value="" placeholder="Price"  readonly>
                        </div>
                    </div>

                    <div class="col-sm-3 nopadding">
                        <div class="form-group">
                            <div class="input-group">
                                    <input type="text" class="form-control total"  name="total[]" value="" placeholder="Total" readonly >
                                <div class="input-group-btn">
                                    <button class="btn btn-success" type="button"  onclick="education_fields();"> <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> </button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="clear"></div>
                    </div>

                </div>
                <div class="panel-footer"><small>Press <span class="glyphicon glyphicon-plus gs"></span> to add another product field :)</small>, <small>Press <span class="glyphicon glyphicon-minus gs"></span> to remove the last product :)</small></div>
            </div>
            <button type="submit" class="btn btn-primary center-block">Order Now </button>
        <?php echo form_close();?>
        </div>
    </div>

</div>

<script>

    var room = 0;
    function education_fields() {

        room++;
        var objTo = document.getElementById('education_fields');
        var divtest = document.createElement("div");
        divtest.setAttribute("class", "form-group removeclass"+room);
        var rdiv = 'removeclass'+room;
        var medo='<div class="col-sm-3 nopadding"><div class="form-group"><select class="selectpicker" data-show-subtext="true" data-live-search="true" name="select_product[]"><option></option><?php  foreach($order as $row){ ?><option data-price="<?php echo$row->p_price;?>"  value ="<?php echo $row->s_p_id; ?>"><?php echo $row->s_p_name; ?></option><?php }  ?></select></div></div><div class="col-sm-3 nopadding"><div class="form-group"> <input type="text" class="form-control"  name="qty[]" value="" placeholder="Quantity"></div></div><div class="col-sm-3 nopadding"><div class="form-group"> <input type="text" class="form-control price"   name="price[]" value="" placeholder="Price"  readonly ></div></div><div class="col-sm-3 nopadding"><div class="form-group"><div class="input-group"> <input type="text"  class="form-control total"  name="total[]" placeholder="Total" readonly /><div class="input-group-btn"> <button class="btn btn-danger" type="button" onclick="remove_education_fields('+ room +');"> <span class="glyphicon glyphicon-minus" aria-hidden="true"></span> </button></div></div></div></div><div class="clear"></div>';

        divtest.innerHTML = medo;
        objTo.appendChild(divtest);
        $('select').selectpicker();
    }
    function remove_education_fields(rid) {
        $('.removeclass'+rid).remove();
    }
</script>
<script> // dodal area 
$(document).on('mouseout','.price',function(){
  var quantity  = $(this).parent().parent().prev().find('input[type="text"]').val();
  var price     = $(this).val();
  if(quantity !=='' && price !==''){
    $(this).parent().parent().next().find('input[type="text"]').val(parseInt(quantity) * parseInt(price));
  }
});

$(document).on('mouseout','input[placeholder="Quantity"]',function(){
  var price     = $(this).parent().parent().next().find('input[type="text"]').val();
  var quantity  = $(this).val();
  if(quantity !=='' && price !==''){
    $(this).parent().parent().next().next().find('input[type="text"]').val(parseInt(quantity) * parseInt(price));
  }
});

</script>
<script>
    function set_price( slc ) {
        var price = slc.find(':selected').attr('data-price');
        slc.parent().parent().next().next().find('.price').val(price);
    }
    $('#education_fields').on('change','select.selectpicker',function(){
        set_price( $(this) );
    });

</script>


<script src="<?php echo base_url();?>js/bootstrap-select.min.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="<?php echo base_url();?>js/bootstrap.min.js"></script>

<!-- Metis Menu Plugin JavaScript -->
<script src="<?php echo base_url();?>js/metisMenu.min.js"></script>

<!-- Custom Theme JavaScript -->
<script src="<?php echo base_url();?>js/startmin.js"></script>

</body>
</html>

如果不可能如何在PHP中执行此操作,如果它找到重复的值进行求和并将其作为一行插入。

2 个答案:

答案 0 :(得分:0)

如果您只想在前端阻止用户,您可以为每个选定的项目设置data-id属性,如果用户单击要添加到列表的项目,您可以搜索具有data-id条件的所有项目,如果是重复项则阻止它

答案 1 :(得分:0)

使用 data属性是个好主意,但是我分享了我想验证选择控件的选择解决方案,该控件使用另一个先前的行select中已经存在的选项来进行验证。实际上,我使用的是 jquery.repeater 插件,而不是HTML表或DataTable。

function selectValueDuplicated(selector, value) {
    var checker = {};
    $(selector).each(function(index, element) {
        console.log('index:', index);
        console.log('element:', element);

        var selection = $(this).val();
        if ( checker[selection] ) {
            alert("Duplicate(s) detected!");
            return false;
        } else {
            checker[selection] = true;
        }
    });
    console.log(checker);
}