开发者

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

开发者 https://www.devze.com 2023-02-25 18:57 出处:网络
I want to send a post from HTML that contains info about a certain form to a controller in code igniter. The I want the controller to process the info and loads a certain page inside a div. Here is my

I want to send a post from HTML that contains info about a certain form to a controller in code igniter. The I want the controller to process the info and loads a certain page inside a div. Here is my code. I think I'm supposed to use something like .html or something? I'm not quite sure, I dont understand it

The controller

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

    }

the form in 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>

the jquery function

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



        $.post("<?ph开发者_运维技巧p 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


Firstly, in your controller change this line like this (u need to pass the string name of the field here):

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

Next, change your jQuery to be like this:

$("#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

You cant call your view directly, and you don't need to. The post should return the data, which you can write out to your #insert-activity element.

0

精彩评论

暂无评论...
验证码 换一张
取 消