简体   繁体   中英

PHP (Codeigniter) and ajax help

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;
});

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.

Nowadays, you can just use $this->input->is_ajax_request() , available in the input class , to achieve the same results as with the hand-made helper by @ifaour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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