$(document).ready(function() {
 $('#button').click(function() {
 $.ajax({
             type: "POST",
             url: "{:U('Procurement/product_add')}",
             data: "text",
             cache:false,
             dataType: "json",
              error: function(){ //如果出错,执行函数   
                 alert('Error loading XML document');   
             }, 
             success: function(data){
                 alert(data);//如果成功,弹出数据   
             }
         });
     });
});Public function product_add(){
                            // if(!IS_AJAX){
                            //     $this->error("页面不存在");
                            // }
                     if(IS_AJAX) halt("页面不存在");
                            $data = array(
                                    'product_name' => I('post.product_names'),
                                    'State' => I('post.State'),
                                    'Remarks' => I('post.Remarks'),
                                    'product_code' => I('post.product_code'),
                                    'protection' => I('post.protection'),
                                    'Selling_price' => I('post.Selling_price'),
                                    'latest_cost' => I('post.latest_cost'),
                                    'Tax' => I('post.Tax'),
                                    'lowest_price' => I('post.lowest_price'),
                                    'Stock' => I('post.Stock'),
                                    'Packaging_unit' => I('post.Packaging_unit'),
                                    'Weight' => I('post.Weight'),
                                    'number_units' => I('post.number_units'),
                                    'Specifications' => I('post.Specifications'),
                                    'Packing_size' => I('post.Packing_size'),
                                    'images' => I('post.images'),
                        );
                            $Product_name = M('product_name');
                            $result = $Product_name->add($data);
                             // echo $Product_name->getLastSql();
                            if($result){
                                //获取到主建返回的id
                                $id = $result;
                                //实例化附表
                                $product_name_class = M('product_name_class');
                                //用一个数组接受附表的字段,eid是主表返回的id
                                $data = array(
                                    'product_class'=>I("post.product_class"),
                                    'eid'=>$id,
                                    'Brand'=>I("post.Brand"),
                                    );
                                //附表添加
                                $results = $product_name_class->add($data);
                                // echo $product_name_class->getLastSql();
                                if($results){
                                  echo json_encode($data);
                                }
                            }else{
                                $this->ajaxReturn(0,"新增错误",0);
                            }		最佳答案