Gọi ajax cho PHP (đang thực hiện framework Phalcon)

by Wednesday, February 24, 2016 0 comments
$('#read-more').on('click', function() {
        var dataajax = {'read-more-from': $("#read-more-from").val()};
        // gọi ajax
        $.ajax({
            url : "/news/readmore",
            type : 'POST',
            data : dataajax,
            dataType : 'json',
            success: function(data){
                // gọi function append vào html
                addNewsAjax(data.arrNews);
                $("#read-more-from").val(data.offset);
            }
        });
    });

=================================

public function readmoreAction() {
        if ($this -> request -> isPost()) {
            $this -> view -> disable();
            // lấy dữ liệu truyền thông qua ajax
            $offset = $this->request -> getPost('read-more-from');

            $news = new News();
            // lấy dữ liệu từ DB
            $newsList = $news -> findByLimit(10, $offset);
            // khai báo array chứa dữ liệu để loop ngoài html
            $arrNews = array();
            foreach ($newsList as $news) {
                $arr = array('id'             => $news -> id,
                                'title'              => $news -> title,
                                'description'   => $news -> description,
                                'content'         => $news -> content,
                                'url'                => $news -> url,
                                'image'           => $news -> image,
                                'updated_at'   => date('d-m-Y', strtotime($news -> updated_at)));
                array_push($arrNews, $arr);
            }
            // output json ra ngoài javascript
            echo json_encode(array('arrNews' => $arrNews, 'offset' => ($offset + 10)));
        }
    }
==================================
<a href="javascript:void(0)" id="read-more">Xem tiếp >></a>

Unknown

Developer

Notes này để lưu lại những vấn đề, những công việc trong quá trình làm việc gặp phải, để sau này khi gặp phải thì có thể giải quyết nhanh chóng. Ngoài ra thì còn những thông tin khác để giúp lưu lại những kiến thức đã biết.

0 comments:

Post a Comment