0

我的控制器代码

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Welcome extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('welcome_model');
    }

    public function index() {
        $data = array();
        $data['category'] = $this->welcome_model->select_category();
        $data['product'] = $this->welcome_model->select_product();
        $this->load->view('purchase_entry', $data);
    }

    function get_products($category) {
        header('Content-Type: application/x-json; charset=utf-8');
        echo(json_encode($this->welcome_model->select_product_by_category($category)));
    }

    public function add() {
        $result = $this->welcome_model->select_product_by_id($this->input->post('product_id', TRUE));


        $insert = array(
            'id' => $this->input->post('product_id', true),
            'qty' => $this->input->post('quantity', true),
            'price' => $this->input->post('unit_price', true),
            'name' => $result->product_name
        );
        $this->cart->insert($insert);
        redirect("welcome/index");
    }

}

我的模型课

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome_Model extends CI_Model {

    public function select_category()
    {
        $this->db->select('*');
        $this->db->from('category');
        $query_result=  $this->db->get();
        $result=$query_result->result();
        return $result;
    }

    public function select_product()
    {
        $this->db->select('*');
        $this->db->from('product');
        $query_result=  $this->db->get();
        $result=$query_result->result();
        return $result;
    }

    public function select_product_by_category($category_id)
    {
        $this->db->select('*');
        $this->db->from('product');
        $this->db->where('category_id',$category_id);
        $query_result=  $this->db->get();
        $result=$query_result->result();
        return $result;
    }
    public function select_product_by_id($product_id)
    {
        $this->db->select('*');
        $this->db->from('product');
        $this->db->where('product_id',$product_id);
        $query_result=  $this->db->get();
        $result=$query_result->row();
        return $result;
    }

}

?>

我的视图文件是

<html>
<head>
    <title></title>

    <script type="text/javascript" src="<?php echo base_url(); ?>js/jquery-1.7.1.min.js"></script>
    <script>   
        $(document).ready(function(){
            $('#category_id').change(function(){ 
                $("#product_id > option").remove(); 
                var category_id = $('#category_id').val();
                $.ajax({
                    type: "POST",
                    url: "http://localhost/test_cart/index.php/welcome/get_products/"+category_id, 

                    success: function(cities) 
                    {
                        $.each(cities,function(id,name) 
                        {
                            var opt = $('<option />'); 
                            opt.val(id.product_id);
                            opt.text(name.product_name);
                            $('#product_id').append(opt); 


                        });
                    }

                });

            });
        });
    </script>
    <style type="text/css">
        a{text-decoration: none;}
    </style>
</head>
<body>
    <form action="<?php echo base_url(); ?>welcome/add" method="post">
        <table width="400px" align="center">
            <caption><h3>Purchase Invoice</h3></caption>
            <tr>
                <td>Invoice Date :</td>
                <td>
                    <input type="text" name="invoice_date" value=""/>
                </td>
            </tr>

            <tr>
                <td>Select Category :</td>
                <td>
                    <select name="category_id" id="category_id">
                        <option>--- Select Category ---</option>
                        <?php foreach ($category as $aCategory) { ?>
                            <option value="<?php echo $aCategory->category_id; ?>"><?php echo $aCategory->category_name; ?></option>

                        <?php } ?>
                    </select>
                </td>
            </tr>

            <tr>
                <td>Select Product :</td>
                <td>
                    <select name="product_id" id="product_id">
                        <option>--- Select Product ---</option>


                    </select>
                </td>
            </tr>

            <tr>
                <td>Unit Price :</td>
                <td>
                    <input type="text" name="unit_price" value=""/>
                </td>
            </tr>

            <tr>
                <td>Quantity :</td>
                <td>
                    <input type="text" name="quantity" value=""/>
                </td>
            </tr>

            <tr>
                <td>&nbsp;</td>
                <td>
                    <input type="submit" value="Submit"/>
                </td>
            </tr>
        </table>
    </form>
    <form action="" method="post">
    <h2 align="center">Invoice Details</h2>
    <table border="1px" cellpadding="5px" cellspacing="1px" style="margin-top:10px;border-collapse:collapse" width="500px" align="center">
        <tr align="center" style="font-family:Arial, Helvetica, sans-serif; font-size:12px;">
            <th>Serial</th>
            <th>Product Name</th>
            <th>Quantity</th>
            <th>Unit Price</th>
            <th>Amount</th>
            <th>Action</th>
        </tr>
        <?php if ($cart = $this->cart->contents())  ?>
        <?php
        $i = 1;
        foreach ($cart as $item) {
            ?>
            <tr>
                <td><?php echo $i++; ?></td>
                <td><?php echo $item['name']; ?></td>
                <td><?php echo $item['qty']; ?></td>
                <td><?php echo $item['price']; ?></td>
                <td><?php echo $item['subtotal']; ?></td>
                <td>
                    <a href="">Update</a>&nbsp;|&nbsp;<a href="">Remove</a>

                </td>
            </tr>
        <?php } ?>
    </table>
    </form>
</body>

我的问题是,当我想将产品添加到购物车时,控制器的 add 方法无法从帖子中找到参数我的意思是 product_id 没有从视图值传递。如何将选项值从视图传递到控制器作为参数?

4

1 回答 1

0

我不确定我是否理解这个问题。如果您正在执行 ajax 发布和动态更新表单,您可能需要考虑使用 json_encode (php) 和 $.parseJson (jQuery) 将参数发送回客户端。这样您就可以将现成的 html 直接发送到浏览器 ID。

javascript 中的城市函数让我有点困惑。参数是:success:function (data, textStatus, jqXHR)

ajax.post 函数参数是: jQuery.post( url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] ) 所以,数据类似于: var data = {product_id:$(' #product_id').val()}; 这会将数据作为 POST 变量发送,而不是像代码中那样作为 GET 变量发送。

一些评论:在视图中,结束更改行:

<form action="<?php echo base_url(); ?>welcome/add" method="post">

进入:

<form action="<?php echo base_url('index.php'.DIRECTORY_SEPARATOR.'welcome'.DIRECTORY_SEPARATOR.'add'); ?>" method="post">

在模型中,没有 ?> 结束标签,这一切都变成:(这对我有用!)

控制器:

<?php

if (!defined('BASEPATH'))
exit('No direct script access allowed');

class Welcome extends CI_Controller {

public function __construct() {
    parent::__construct();
    $this->load->model('welcome_model');
}

public function index() {
    $data = array();
    $data['category'] = $this->welcome_model->select_category();
    $data['product'] = $this->welcome_model->select_product();
    $this->load->view('purchase_entry', $data);
}

function get_products($category) {

    header('Content-Type: application/x-json; charset=utf-8');
    echo(json_encode($this->welcome_model->select_product_by_category($category)));
}

function add() {
    $result = $this->welcome_model->select_product_by_id($this->input->post('product_id', TRUE));


    $insert = array(
        'id' => $this->input->post('product_id', true),
        'qty' => $this->input->post('quantity', true),
        'price' => $this->input->post('unit_price', true),
        'name' => $result->product_name
    );
    $this->cart->insert($insert);
    redirect("welcome/index");
}

}

模型:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

 class Welcome_Model extends CI_Model {

public function select_category()
{
    $this->db->select('*');
    $this->db->from('category');
    $query_result=  $this->db->get();
    $result=$query_result->result();
    return $result;
}

public function select_product()
{
    $this->db->select('*');
    $this->db->from('product');
    $query_result=  $this->db->get();
    $result=$query_result->result();
    return $result;
}

public function select_product_by_category($category_id)
{
    $this->db->select('*');
    $this->db->from('product');
    $this->db->where('category_id',$category_id);
    $query_result=  $this->db->get();
    $result=$query_result->result();
    return $result;
}
public function select_product_by_id($product_id)
{
    $this->db->select('*');
    $this->db->from('product');
    $this->db->where('product_id',$product_id);
    $query_result=  $this->db->get();
    $result=$query_result->row();
    return $result;
}


}

看法

<html>
<head>
<title></title>

<script type="text/javascript" src="<?php echo base_url(); ?>js/jquery.min.js"></script>
<script>   
    $(document).ready(function(){
        $('#category_id').change(function(){ 
            $("#product_id > option").remove(); 
            var category_id = $('#category_id').val();
            $.ajax({
                type: "POST",
                url: "<?php echo base_url('index.php/welcome/get_products/').'/';?>"+category_id, 

                success: function(cities) 
                {
                    $.each(cities,function(id,name) 
                    {
                        var opt = $('<option />'); 
                        opt.val(id.product_id);
                        opt.text(name.product_name);
                        $('#product_id').append(opt); 


                    });
                }

            });

        });
    });
</script>
<style type="text/css">
    a{text-decoration: none;}
</style>
</head>
<body>
<form action="<?php echo base_url('index.php'.DIRECTORY_SEPARATOR.'welcome'.DIRECTORY_SEPARATOR.'add'); ?>" method="post">
    <table width="400px" align="center">
        <caption><h3>Purchase Invoice</h3></caption>
        <tr>
            <td>Invoice Date :</td>
            <td>
                <input type="text" name="invoice_date" value=""/>
            </td>
        </tr>

        <tr>
            <td>Select Category :</td>
            <td>
                <select name="category_id" id="category_id">
                    <option>--- Select Category ---</option>
                    <?php foreach ($category as $aCategory) { ?>
                        <option value="<?php echo $aCategory->category_id; ?>"><?php echo $aCategory->category_name; ?></option>

                    <?php } ?>
                </select>
            </td>
        </tr>

        <tr>
            <td>Select Product :</td>
            <td>
                <select name="product_id" id="product_id">


                </select>
            </td>
        </tr>

        <tr>
            <td>Unit Price :</td>
            <td>
                <input type="text" name="unit_price" value=""/>
            </td>
        </tr>

        <tr>
            <td>Quantity :</td>
            <td>
                <input type="text" name="quantity" value=""/>
            </td>
        </tr>

        <tr>
            <td>&nbsp;</td>
            <td>
                <input type="submit" value="Submit"/>
            </td>
        </tr>
    </table>
</form>
<form action="" method="post">
<h2 align="center">Invoice Details</h2>
<table border="1px" cellpadding="5px" cellspacing="1px" style="margin-top:10px;border-collapse:collapse" width="500px" align="center">
    <tr align="center" style="font-family:Arial, Helvetica, sans-serif; font-size:12px;">
        <th>Serial</th>
        <th>Product Name</th>
        <th>Quantity</th>
        <th>Unit Price</th>
        <th>Amount</th>
        <th>Action</th>
    </tr>
    <?php if ($cart = $this->cart->contents())  ?>
    <?php
    $i = 1;
    foreach ($cart as $item) {
        ?>
        <tr>
            <td><?php echo $i++; ?></td>
            <td><?php echo $item['name']; ?></td>
            <td><?php echo $item['qty']; ?></td>
            <td><?php echo $item['price']; ?></td>
            <td><?php echo $item['subtotal']; ?></td>
            <td>
                <a href="">Update</a>&nbsp;|&nbsp;<a href="">Remove</a>

            </td>
        </tr>
    <?php } ?>
</table>
</form>
</body>
</html>
于 2013-07-29T13:25:35.747 回答