发送美元.在代码点火器中从JQuery发送到控制器并加载结果


Send $.post from JQuery to controller in code igniter and load the result

我想从HTML发送一个帖子,其中包含有关某个表单的信息,以代码点火器控制器。我希望控制器来处理信息,并加载一个div内的特定页面。这是我的代码。我想我应该用。html之类的?我不太清楚,我不明白。

控制器

function search_friend(){
    //this function gets text from text field and searches for a user and returns all users similar to this dude
    // $this->input->post($searchFriendForm);
    // $this->input->post($searchFriendText);
    $this->load->model('userProfile_m');
    $people = $this->userProfile_m->get_user_by_name($this->input->post($searchFriendText));
    $this->load->view('addFriendSearchResult',$people);
    }

html

中的表单
<form method="post" action="" name="searchFriendForm" id="add-friend-search">
<input type="text"/ name="searchFriendText">
<input type="button" class="small green button" id="add-friend-button" />

</form>

jquery函数

$("#add-friend-button").click(function(){ //start click

        $.post("<?php echo site_url('userProfile/search_friend'); ?>", $("#add-friend-search").serialize());
        $("#insert-activity").load("<?php echo base_url().''?>system/application/views/addFriendSearchResult.php");
        $("#add-friend-search").slideUp("slow",function(){});
    }); //end click

首先,在您的控制器中像这样更改这一行(您需要在这里传递字段的字符串名称):

$people = $this->userProfile_m->get_user_by_name($this->input->post('searchFriendText'));

接下来,像这样修改jQuery:

$("#add-friend-button").click(function(){ //start click
    $.post("<?php echo site_url('userProfile/search_friend'); ?>", 
        $("#add-friend-search").serialize(),
        function(data){
           $("#insert-activity").html(data);
        });
    $("#add-friend-search").slideUp("slow",function(){});
}); //end click

你不能直接调用视图,你也不需要这样做。post应该返回数据,您可以将其写入#insert-activity元素。