Sobin Augustine
Sobin Augustine

Reputation: 3775

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.

Upvotes: 0

Views: 713

Answers (4)

Padmanathan J
Padmanathan J

Reputation: 4620

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>

Upvotes: 2

kemal
kemal

Reputation: 371

Does the data correctly ?

var obj = jQuery.parseJSON(data);

use JSON in the form of.I hope the right understand

Upvotes: 0

Vikash Kumar
Vikash Kumar

Reputation: 665

In this kind of situation I used to make table in controller itself and assign it to variable. So you can get that table in View as AJAX Response. Then its very simple to assign response to inner HTML of resource Id where its required to display.

Upvotes: 0

Antoine
Antoine

Reputation: 5691

PHP is a server-side language. If you want to use PHP data in your view, you need to convert it to a client-side language like Javascript. For example, in your display_info controller, you could return some JSON, using PHP's json_encode to convert a PHP array made of useful data for your view. Output it with the application/json content-type header.

Upvotes: 1

Related Questions