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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)