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 に答える