duanhe7471 2011-01-28 13:28
浏览 41

PHP(Codeigniter)和ajax帮助

I am trying to create a twitter like application in which the user enters some data into a form and through using ajax (jquery library) the users see theirs submission in realtime go to the top of the all the other data.

The way it works is the user submits the form and the data gets submitted to the database, I also want to add teh data to the list of data using ajax.

My problem is I can only access the data the PHP method creates from the ajax request if I echo $var; in my php method, this doesn't look correct to me can some tell me what I am doing wrong please?

    public function feed() {
        $this->load->library('form_validation');
        $this->load->helper('dates');
        $data['feed'] = $this->f->get_feed_by_employer($this->session->userdata('employer_id'));
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        $this->form_validation->set_rules('content', 'content', 'required|trim|max_length[140]');
        $this->form_validation->set_rules('retrain', 'retrain position', 'trim|max_length[1]');

        if ($this->form_validation->run() == FALSE)
        {
            echo validation_errors('<div class="error">', '</div>');
            $this->template->build('employer/feed', $data);
        }
        else
        {
            $insert = array(
                'content' => $this->input->post('content'), 
                'retrain' => $this->input->post('retrain'),
                'created_at' => time(),
                'employers_id' => $this->session->userdata('employer_id')
            );

            if($this->f->insert($insert)) {
                echo $insert['content'];
            }
        }
}

and the jquery

$('#employer_feed').submit(function(){
    $.ajax({
        url: '/employer/feed',
        data: $('#employer_feed').serialize(),
        type: 'POST',
        success:function(html) {
            $('#feed').append('<div class="feed_item">'+html+'</div>');
        }
    });
    return false;
});
  • 写回答

2条回答 默认 最新

  • douxun1934 2011-01-28 13:54
    关注

    There's no problem of using echo when dealing with ajax request, actually it's the way to go. Also you may use echo json_encode($output); depending on your ajax request type.

    Check this article, using is_ajax to know when to echo and when to load your views is a clean way!

    so create a file is-ajax_helper.php in application/helpers/ folder:

    <?php
    
    function is_ajax(){
        return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) 
                && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');  
    }
    

    And in your config/autoload.php:

    $autoload['helper'] = array('is-ajax');
    

    And just check if it's an ajax call or not!

    EDIT:
    Since Codeigniter 2.0 is officially out and plugins are now replaced with helpers, I have updated my Answer.

    评论

报告相同问题?

悬赏问题

  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形