Q:

how to access the returned php variable using ajax, as a php variable in the view

i am in this situation,
//view
 $.ajax({type: 'POST',
    url: base_url+"home/display_info/"+patient_id,
    async: false,
    success: function(data){
            //alert(data);// alert was working
            }
    });

//controller
function display_info($id)
    {
        $document= $this->document_model->getDocumentOfPatient($id);
        print_r($document);
    }

in this i am getting the data as an array from the controller, and i want to get the data to a php array variable to build a table(html) with that array,but stuck here, is there any way to set a table(html) with this returned data variable, can i access the variable <?php echo $document['document_id'];?> like this in the view.

A:

Try this
First you create table in your view page. Table id name foo and use to create table row and append to the html table
Sample code is given below
    <scrit type="text/javascript">
     $.ajax({type: 'POST',
        url: base_url+"home/display_info/"+patient_id,
        async: false,
        success: function(data){
              var table = '<tr><td>' + data['patient_id'] + '</td><td>' + data['document_id'] + '</td><td>' + data['document_date'] + '</td><td>'+ data['insert_user_id']+  '</td></tr>';

        $('#poo > tbody').append(table);
                }
        });

    </script>
    <table id="poo" style="margin:10px 0px 0px 0px;" width="100%" border="0" cellpadding="0" cellspacing="0">
        <thead>
            <tr>
                <td><strong>Product id</strong></td>
                <td><strong>Doc id</strong></td>
                <td><strong>Date</strong></td>
                <td><strong>userid</strong></td>
            </tr>
        </thead>
<tbody>
</tbody>   
    </table>

