【问题标题】:Send $.post from JQuery to controller in code igniter and load the result将 $.post 从 JQuery 发送到代码点火器中的控制器并加载结果
【发布时间】:2011-04-16 12:44:43
【问题描述】:

我想从 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

【问题讨论】:

    标签: php jquery ajax codeigniter


    【解决方案1】:

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

    $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
    

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

    【讨论】:

      猜你喜欢
      • 2013-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-15
      • 1970-01-01
      • 2012-12-25
      • 1970-01-01
      相关资源
      最近更新 更多