0

这是交易,我创建了一个搜索结果页面,我想打开一个模式并通过 php 中的 ajax 检索配置文件数据,但我不太确定如何实现它。我已经创建了模态,但我不知道如何从单击搜索结果中获取“id”,将其传递给 ajax 以检索配置文件详细信息。

Twitter 有这种功能。当您单击提要中的用户名时,它会打开一个模式并为您提供个人资料的简要概述以及指向完整个人资料的链接。

如果有人可以帮助我或为我指明正确的方向,那对我来说意义重大!:)

PS我正在使用codeigniter,如果有人想知道我正在使用什么框架。

编辑:这是代码

<?php
/*** PHP Search Results Loop ***/
if ($num > 0){
foreach ($query->result_array() as $row)
{
    echo "link to ajax"'>";
}
} else {
echo "<strong>results not found :(</strong>";
}?>


<script> /* jQuery Runs When Result Has Been Clicked */
    $('#ResultText').click(function() {

        var targetid = {
            id: /*Profile Id*/,
            ajax: '1'
        };

        /* Modal Code */
        $('.modal-backdrop, .modal-profilebox').css('display', 'block');
        $('.modal-backdrop').animate({'opacity':'.50'}, 100, 'linear');
        $('.modal-profilebox').animate({'opacity':'1.00'}, 200, 'linear');

        $.ajax({
            url: "<?php echo site_url('finder/modal'); ?>",
            type: 'POST',
            data: form_data,
            success: function(profileData) {
                $('.modal-profilebox').html(profileData);
            }
        });

        return false;
    });
</script>
4

1 回答 1

1

尝试这样的事情

public function ajax_user_profile($user_id)
{
    if( !$this->input->is_ajax_request() )
    {
        redirect('/'); //only allow ajax requests
    }

    try
    {
        $user_profile = ''; //grab user object from DB

        if( !$user_profile )
            throw new Exception("Error Message");
    }
    catch(Exception $e)
    {
        log_message('error', $e->getMessage());
        echo json_encode(array(
            'error' =>  1
        ));
        exit;
    }

    $data = $this->load->view(array(
        'some_view' =>  'some_view',
        'user'      =>  $user_profile
    ), TRUE); //NO Template, just the view by adding TRUE

    echo json_encode(array(
        'error' =>  0,
        'data'  =>  $data
    )); //return view data

}

--

<a rel="modal" data-user-id="1" >Joe Bloggs</a>

--

(function($){

    var userObj = {

        init : function(){
            this.getProfile();
        },
        getProfile : function(){

            var modalSearch = $("a[rel='modal']");

            modalSearch.on('click', function(){

                //trigger the modal window

                //request ajax
                $.ajax({
                    url : BASEPATH + 'users/profile/' + $(this).data('user-id'),
                    type : 'POST',
                    dataType: 'json',
                    success : function(callback){
                        if(callback.error ==0)
                        {
                            $('.modal-profilebox').html(callback.data);
                        }
                    }
                });
            });

        }
    }

    $(function(){
        userObj.init()
    });
})(jQuery);
于 2012-11-20T00:24:01.987 回答