简体   繁体   English

在cakephp中使用Ajax不起作用

[英]Using Ajax with cakephp doesnt work

I have a page like this: 我有一个这样的页面: 在此处输入图片说明

Basically, I pick 2 dates and hit the button, then the data below will change without refreshing this page. 基本上,我选择2个日期并单击按钮,然后下面的数据将更改而无需刷新此页面。

Here is the code in controller: 这是控制器中的代码:

if( $this->request->is('ajax') ) {

        $this->autoRender = false;

        //if ($this->request->isPost()) {
            print_r($this->request->data);
            // get values here 
            echo $from=( $this->request->data('start_time'));
            echo $to= $this->request->data('end_time');
            Debugger::dump($from);
            Debugger::dump($to);


        //$this->layout = 'customer-backend';
        $this->Order->recursive=-1;
        $this->Order->virtualFields['benefit']='SUM(Product.product_price - Discount.product_discount)';
        $this->Order->virtualFields['number']='COUNT(Order.order_id)';
        $option['joins'] = array(
            array('table'=>'discounts',
                'alias'=>'Discount',
                'type'=>'INNER',
                'conditions'=>array(
                    'Order.discount_id = Discount.discount_id',
                )
            ),
            array('table'=>'products',
                'alias'=>'Product',
                'type'=>'INNER',
                'conditions'=>array(
                    'Discount.product_id = Product.product_id'
                )
            )
        );
        $option['fields']= array('Discount.product_id','Product.product_name','benefit','number');
        $option['conditions']=array('Discount.start_time >='=>$from);
        $option['group'] = array('Discount.product_id','Product.product_name');
        //$option['limit']=20;
        $products = $this->Order->find('all',$option);
        //Debugger::dump($products);
        $this->set('products',$products);
    //}
    }
    else
    {
        $from='27 November 2012';
        //$this->layout = 'customer-backend';
        $this->Order->recursive=-1;
        $this->Order->virtualFields['benefit']='SUM(Product.product_price - Discount.product_discount)';
        $this->Order->virtualFields['number']='COUNT(Order.order_id)';
        $option['joins'] = array(
            array('table'=>'discounts',
                'alias'=>'Discount',
                'type'=>'INNER',
                'conditions'=>array(
                    'Order.discount_id = Discount.discount_id',
                )
            ),
            array('table'=>'products',
                'alias'=>'Product',
                'type'=>'INNER',
                'conditions'=>array(
                    'Discount.product_id = Product.product_id'
                )
            )
        );
        $option['fields']= array('Discount.product_id','Product.product_name','benefit','number');
        $option['conditions']=array('Discount.start_time >='=>$from);
        $option['group'] = array('Discount.product_id','Product.product_name');
        //$option['limit']=20;
        $products = $this->Order->find('all',$option);
        $this->set('products',$products);
    }

If the request is ajax, it gets 2 values $from and $to from the POST and pass them to the SQL query. 如果请求是ajax,它将从POST获取2个值$from$to并将它们传递给SQL查询。 If the request is not ajax (mean the access this page for the first time when the dates havent picked yet), $from and $to are assigned default values. 如果请求不是ajax(表示尚未选择日期,则是第一次访问该页面),将为$ from和$ to分配默认值。

Here is my ajax in view: 这是我的ajax:

<script>
$(function(){
    $('#btnSubmit').click(function() {
    var from = $('#from').val();
    var to = $('#to').val();
    alert(from+" "+to);
    $.ajax({
        url: "/project/cakephp/orders/hottest_products",
        type: 'POST',

        data: {"start_time": from, "end_time": to },
        success: function(data){
            alert("success");
        }
    });
});
});

it gets data from 2 date picker then send it to the controller as a POST method. 它从2日期选择器获取数据,然后将其作为POST方法发送到控制器。

My problem is that after I choose 2 dates and hit the button, nothing happens. 我的问题是选择2个日期并单击按钮后,什么也没发生。 the data doesnt change according to the dates. 数据不会根据日期更改。 Any thoughts about this. 关于此的任何想法。 Thanks in advance. 提前致谢。

When opening your page and running the following in the console: 打开页面并在控制台中运行以下命令时:

$(".tab_container").html("loaded from ajax");

The products table now only shows "loaded from ajax". 现在products表仅显示“从ajax加载”。 If the content of the products table is generated by it's own template you can have cakephp render that template only when it's an ajax call: http://book.cakephp.org/2.0/en/controllers.html 如果products表的内容是由其自己的模板生成的,则只有当ajax调用时,cakephp才能使该模板呈现该模板: http : //book.cakephp.org/2.0/en/controllers.html

$this->render('/Path/To/ProductTable/');

If your cakephp will output only the product table when an ajax call is made you could try to run the following code: 如果您的cakephp在进行ajax调用时仅输出产品表,则可以尝试运行以下代码:

var from = "2000-01-01";
var to = "2014-01-01";
$.ajax({
    url: "/project/cakephp/orders/hottest_products",
    type: 'POST',
    data: {"start_time": from, "end_time": to }
}).then(
  function(result){
    $(".tab_container").html(result);
  },function(){
    console.log("fail",arguments);
  }
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM