Access to specific array object from ajax call

Hello, I’m doing an ajax call to fill a form using an “ID”

Ajax Call

$(document).ready(function () {
    $("#searchbtn").click( function() {         
         var ncmrid = $('#search').val();
            url: "http://localhost/ncmr/index.php/ncmr/get_ncmr_by_id",
            async: false,
            type: "POST",
            data: "idncmr="+ncmrid,
            dataType: "html",
            success: function(data) {



<input type="text" class="form-control" placeholder="Search by NCMR ID" id="search" />
<button class="btn btn-default" type="button" id="searchbtn">Search</button>

<div id="form">


Basically my controller get information from a table using the ID

$id = $this->input->post('idncmr');
$data['ncmr'] = $this->ncmr_model->get_ncmr_by_id($id);

The result is loaded on the DIV as

Array ( [0] => stdClass Object ( [ID] => 1 [Plant] => 3 [Id_ncmr] => 4781 [Description] => SCRAP [Submitby] => xxx [Submit_date] => 2011-02-09 [Promise_date] => 2011-02-16 [Close_date] => 0000-00-00 [Program] => 532 [Partnum] => 218 [Found_in] => 4 [Status] => 1 ) ) 

I would like to put the ID, Plant,etc values on an Input text

I know how to put all the array on an input text with

success: function(data) {

But I would like to put individual values on different input texts, anyone know how to access to the individual values on this example?

thank you.

To do this, your controller should return something that JavaScript can understand.
If you want to return array, use JSON instead of plain text with print_r:

header('Content-Type: application/json');
echo json_encode($data['ncmr']);

Then change dataType in $.ajax to JSON:

dataType: 'json',

After that you will be able to access array fields in your callback:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.