3

首先对不起,我是一个非常大的初学者。

我遇到了一个我无法真正解决的问题,我正在建造一个类似 facebook 的墙。发送帖子后,我希望将结果附加到 div 中,但我完全迷失了。

问题是它发送完美,但没有显示结果

这是我的代码

html

<?php foreach($stream_posts as $sp): ?>
    <div id="sp_<?php echo $sp->sid; ?>" class="row stream-posts">
        <div class="span1 stream-thumb">
            <ul class="thumbnails">
              <li>
                <a href="#" class="thumbnail">
                  <img src="http://placehold.it/60x60" alt="">
                </a>
              </li>
            </ul>
        </div>  
        <div class="span5 stream-content">
            <a href="#" class="author"><?php echo $sp->first_name ." ". $sp->last_name; ?></a>
            <p>
                <?php echo nl2br($sp->text); ?>
            </p>
        </div>

    </div>
    <?php endforeach; ?>

jQuery

$('.stream-form').on('submit', function(){
        var streamData = $(this).serialize();
        $.ajax({
            url: "<?php echo base_url(); ?>stream/insert_stream",
            type: "POST",
            data: streamData,
            dataType: "json",
            success: function(stream) {

                if(stream.error == 0)
                {
                    $('.stream-text').val("");
                    $('.stream-posts').append().text(stream.text);


                }   
            }
        });
        return false;
    });

在这个 jQuery 代码中,它附加了所有内容并打破了 div,我想向新帖子显示提交的内容

模型

    function add_stream()
    {

        $data = array(
            'added_by' => $this->session->userdata('user_id'),
            'text' => $this->input->post('stream_text')
        );

        $this->db->insert('pf_stream', $data);


    }

function load_stream_post()
{
    $this->db->select('*');
        $this->db->from('pf_stream');
        $this->db->join('pf_users', 'pf_users.id = pf_stream.added_by');
        $this->db->order_by('pf_stream.sid', 'DESC');
        $this->db->limit(1);
        $query = $this->db->get();

        if($query->num_rows() > 0)
        {   
            foreach($query->result() as $row)
            {
                $sdata[] = $row;
            }
        return $sdata;  
        }

}
    // this is for the view
    function select_stream()
    {
        $this->db->select('*');
        $this->db->from('pf_stream');
        $this->db->join('pf_users', 'pf_users.id = pf_stream.added_by');
        $this->db->order_by('pf_stream.sid', 'DESC');
        $this->db->limit(5);
        $query = $this->db->get();

        if($query->num_rows() > 0)
        {   
            foreach($query->result() as $row)
            {
                $data[] = $row;
            }
        return $data;   
        }
    }

控制器

 public function index()
   {
        $this->load->view('partials/header');
        $data['stream_posts'] = $this->stream_model->select_stream();
           $this->load->view('stream_view', $data);
        $this->load->view('partials/footer');
   }

    function insert_stream()
    {
        $this->form_validation->set_rules('stream_text', 'Üzenet', 'trim|required|xss_clean');

        if($this->form_validation->run() == false)
        {
            $stream = array('error' => 1);
        } else {
            $this->stream_model->add_stream();
            $data['stream_result'] = $this->stream_model->load_stream_post();
            $stream = array(
               'error' => 0, 
               'sid' => $data['stream_result'][0]->sid,
               'text' => $data['stream_result'][0]->text,
               'author' => $data['stream_result'][0]->first_name . " " . $data['stream_result'][0]->last_name,
            );
        }
        echo json_encode($stream);
    }

有人可以给我一个提示我错过了什么吗?

4

1 回答 1

5

我认为问题在于,这里

$('.stream-posts').append().text(stream.text);

这应该是

$('.stream-posts').append(stream.text);
于 2012-07-08T10:07:45.240 回答