将json数据显示到数据表codeigniter中的另一个页面

时间:2018-03-13 02:26:12

标签: javascript jquery json codeigniter

嗨我在重写json时有问题,如何查看json数据 {" 0":[" 24""翅"" 1""₱7"&# 34;₱7"]}进入数据表? 我在这里有一个视图,它可以保存到数据库我想要的是当用户点击处理{" 0":[" 24"," wings", " 1","₱7","₱7"]}将显示在表格内,如pos页面/第一页。

我的观点(pos_view.php)

<!DOCTYPE html>
<html>
<head>

    <link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/bootstrap/css/bootstrap.css'); ?>">
    <link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/pos_style.css') ?>">
    <script type="text/javascript" src="<?php echo base_url('assets/jquery.js') ?>"></script>
    <script type="text/javascript" src="<?php echo base_url('assets/jquery-ui/jquery-ui.js') ?>"></script>
    <script type="text/javascript">
        $( document ).ready(function() {
            var list = {

                    };
                list = [];
                list2 = ['maine','shar'];
                list3 = ['test', 'test'];
                list[0] = list2;
                list[1] = list3;
                console.log(list);

            $("body").on('load',function autocomplete() {
                $("#item").autocomplete(function(){
                    source: <?php echo $names ?>
                });
            });

            $("#item").on('change paste keyup propertychange ',function() {
                var item_name = $("#item").val();
                var item_info = $("#right");

                $.ajax ({
                    type : 'POST',
                    url : "<?php echo base_url(); ?>/get/item_info",
                    data : {"item_name":item_name},
                    datatype : 'text',
                    success : function(result) {
                        item_info.html(result);
                    }
                });
            });

            $("#add").on('click', function() {
                var item_name = $("#item").val();
                price = $("#price").text();
                quant = $("#quantity").val();
                total = $("#amount_due").text().substr(1);
                if( $("#item").val() !== "") {
                    if (quant !== "" && !isNaN(quant) ) {
                        $.ajax({
                            type : 'POST',
                            url : "<?php echo base_url()?>/get/cart",
                            data : {"price" : price, "quantity" : quant,"item_name" : item_name, "total" : total},
                            datatype : 'text',
                            success : function(result){
                                $('#tbl_head').after(result);
                                $("#quantity").val(' ');
                                $("#item").val(' ');
                            }
                        });

                    }else {
                        alert('Quantity Is Required And Must Be Integer');
                    }
                }else {
                    alert('No Item Selected');
                }
            });

            $('#process').on('click',function () {
                var table = document.getElementById('cart');
                var x = document.getElementById('cart').rows.length;
                var y = document.getElementById('cart').rows[0].cells.length;
                var cart_items = [];
                var total = 0;
                if (x === 1) {
                    alert('Empty Cart!');
                }else {
                    list = [];
                    col_list = [];
                    for (row = 1; row < x; row++){
                        col_list = [];
                        total += parseInt(table.rows[row].cells[4].innerHTML.substr(1));
                        for (col = 0; col < y; col++){
                            col_list.push(table.rows[row].cells[col].innerHTML);
                        }
                        list[row-1] = col_list;

                    }
                    var obj = Object.assign({}, list);
                    cart_list = JSON.stringify(obj);
                    console.log(cart_list);
                    sessionStorage.setItem('cart',cart_list);
                    sessionStorage.setItem('total',total);
                    window.location = "<?php echo base_url() ?>/billing";
                }
            });
        });
    </script>
</head>
<body>


<a href="<?php echo base_url('inventory') ?>"><button style="float: right; margin-top: 6px; margin-right: 20px;" class="btn btn-primary btn-lg" >Sign Out</button></a>

<div class="main">
    <div style="padding: 0 0px;">
        <div class="col-sm-6">
        <h3 style="float: left;">Customer Cart</h3>
        </div>

    </div>
    <div class="clearfix"></div>
    <div class="cart">
        <div id="cart-tbl">
            <table class="table table-striped table-hover" id="cart">
                <tr id="tbl_head">
                    <th>Item ID</th>
                    <th>Item Name</th>
                    <th>Quantity</th>
                    <th>Price</th>
                    <th>Sub Total</th>
                </tr>
            </table>
        </div>
    </div>
    <div class="input">
        <div class="col-sm-6" id="left" >
            <form method="POST">
                <div class="col-sm-12">
                <div class="form-group">
                    <label>Item Name</label>
                    <input id="item" type="text" name="item_name" placeholder="Enter Item Name" class="form-control">
                </div>
                </div>
                <div class="col-sm-8">
                <div class="form-group">
                    <label>Quantity</label>
                    <input id="quantity" type="text" name="quantity" placeholder="Enter Quantity" class="form-control">
                </div>
                </div>
                <div class="col-sm-4" style="padding-top: 0px;">
                    <input style="width: 100%; margin-bottom: 5px;" id="add" type="button" name="enter" class=" btn btn-info input-lg" value="ADD TO CART">
                    <input style="width: 100%;" id="process" type="button" name="process" class=" btn btn-success input-lg" value="PROCESS">
                </div>
            </form>

        </div>
        <div class="col-sm-6" id="right">
            <label id="name">Item Name:</label><br>
            <label id="prc">Price:</label><br>
            <label id="description">Description:</label><br>
        </div>
        <div class="col-sm-6">
        <?php $total_amount = ""; ?>
        <h3 id="total_amount">Total Amount Due <span id="amount_due"></span> </h3>
        </div>
    </div>
</div>
</body>
</html>

下一个/点击进程(billing_view)

<!DOCTYPE html>
<html>
<head>
    <title>POS</title>
    <link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/bootstrap/css/bootstrap.css'); ?>">
    <link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/pos_style.css') ?>">
    <script type="text/javascript" src="<?php echo base_url('assets/jquery.js') ?>"></script>
    <script type="text/javascript" src="<?php echo base_url('assets/jquery-ui/jquery-ui.js') ?>"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            var total = sessionStorage.getItem('total');
            var cart = sessionStorage.getItem('cart');
            $('#amount_due').val('₱' +total);
            $("#cart").val(cart);
            $("#t").val(total);
            console.log($("#cart").val());
            console.log(total);
            $('#payment').on('change paste keyup propertychange',function() {
                payment = $("#payment").val();
                if (isNaN(payment) || payment === undefined) {
                    $("#message").html('<div class="alert alert-danger">Please Enter Numbers Only</div>');
                }
                else if (parseInt(payment) < parseInt(total)) {
                    $("#message").html('<div class="alert alert-danger">Insufficient Amount</div>');
                }else if (parseInt(payment) > parseInt(total)) {
                    change = parseInt(payment) - parseInt(total);
                    $("#message").html('<div class="alert alert-success">Good</div>');
                    $("#c").val(change);
                    $("#change").val('₱' + change);
                }

            });
        });

    </script>
</head>
<body>
<header style="margin-bottom: 40px;color: white;">
<div style="padding-left: 20px; padding-top: 2px;">

    </div>
</header>
<div class="main">

<div class="col-sm-6 col-sm-offset-3">
<?php echo $this->session->flashdata('errorMessage'); ?>
<?php echo form_open('billing/complete'); ?>
<?php echo form_fieldset('<h1 class="text-info">Payment</h1>'); ?>
<div id="message">

</div>
<div class="form-group">
    <label>Total Amount:</label><br>
    <input id="amount_due" type="text" name="" disabled="" class="form-control input-lg">
</div>
<div class="form-group">
    <label>Payment:</label><br>
    <input type="text" id="payment" name="payment" placeholder="Enter Payment" class="form-control input-lg">
</div>
<div class="form-group">
    <label>Change:</label><br>
    <input id="change" type="text" name="" disabled="" class="form-control input-lg">
</div>
<input type="text" name="cart" id="cart">
<input type="hidden" name="change" id="c">
<input type="hidden" name="total" id="t">
<div class="form-group">
    <input type="submit" name="submit" class="form-control btn btn-success input-lg">
</div>
<?php echo form_close(); ?>

</div>
</div>
</body>
</html>

控制器; get.php(控制器所在的地方

  <?php
class Get extends CI_Controller {
    public function __construct() {
        parent::__construct();
        if (!$this->session->userdata('log_in')) {
            $this->session->set_flashdata('errorMessage','<div class="alert alert-danger">Login Is Required</div>');
            redirect(base_url('login'));
        }
    }
    public function item_info() {
        $name = "";
        $price = "";
        $description = "";
        $item_name = $this->input->post('item_name');
        $this->load->model('ajax_model');
        if ($item_info = $this->ajax_model->item_info($item_name)) {
            $name = $item_info->name;
            $price = $item_info->price;
            $description = $item_info->description;
            ?>
            <div class="lb">
            <label id="name">Item Name : <?php echo $name ?></label>
            </div>
            <div class="lb">
                <label id="prc">Price : ₱<span id="price"><?php echo $price ?></span></label>
            </div>
            <div class="lb">
                <label id="description">Description : <?php echo $description ?></label>
            </div>
            <?php
        }else {
            ?>
            <div class="lb">
            <label id="name">No Item Found</label>
            </div>
            <?php
        }



    }

    public function cart() {
        $item_name = $this->input->post('item_name');
        $price = $this->input->post('price');
        $quantity = $this->input->post('quantity');
        $total = $this->input->post('total');
        $this->load->model('ajax_model');

        if ($item_info = $this->ajax_model->item_info($item_name)) {
            $id = $item_info->id;
        $sub_total = (int)substr($price,00) * (int)$quantity;
        $amount_due = (int)$total + $sub_total;
        $cart = [$id,$item_name,$quantity,$price,$sub_total];
        $json = json_encode($cart);
        echo "

        <tr>
            <td>$id</td>
            <td>$item_name</td>
            <td>$quantity</td>
            <td>₱$price</td>
            <td>₱$sub_total</td>
        </tr>

        ";
        echo "<script>

        $('#amount_due').html('₱' +$amount_due);
        </script>";
        }else {
            echo "
            <script>
                alert('No Item Found');
            </script>
            ";

        }

    }

}

?>

和我的结算控制器

    public function index() {

    $this->load->view('billing_view');

}

public function complete() {
    $cart = $this->input->post('cart');
    $total = $this->input->post('total');
    $payment = $this->input->post('payment');
    $change = $this->input->post('change');
    $this->form_validation->set_rules('payment','Payment','required');
    if($this->form_validation->run() == FALSE) {
        $this->session->set_flashdata('errorMessage','<div class="alert alert-danger">' . validation_errors() . '</div>');
        redirect(base_url('billing'));
    }else {

        $data['payment'] = $payment;
        $data['total'] = $total;
        $data['change'] = $change;
        $this->load->model('sales_model');
        $insert_sales = $this->sales_model->insert_sales($cart);

        if ($insert_sales) {
            $this->sales_model->updateStocks($cart);
            $this->load->view('complete',$data);
        }
    }


}

0 个答案:

没有答案