Getting undefined value while returning json value from controller

user_777 Source

Here I want to get all the values which I inserted into the json data in a table before getting submitted but unfortunately am not getting the results I want. So far I have done this. Please have a look.

<script>
  $("#getdata").on('click',function () {
  var form_data={       
                    agent_name: $('#agent_name').val(),
                    number: $('#number').val(),
                    quantity: $('#quantity').val(),
                    date: $('#date').val(),
                    commision: $('#commision').val(),
                    profit: $('#profit').val(),
                    agent_amount: $('#agent_amount').val(),
                    user_id: $('#user_id').val(),
                    type: name_type.val(),
                  }

    $.ajax({
    type: 'POST',
    url: '<?php echo base_url();?>admin_control/ajax_data',
    data: form_data,
    dataType:"json", //to parse string into JSON object,
    success: function(data){ 
        if(data){
            var len = data.length;
            alert(len);
            var txt = "";
            if(len > 0){
                for(var i=0;i<len;i++){
                    if(data[i].number && data[i].type){
                        txt += $('#table').append('<tr><td>data[i].type</td><td>data[i].number</td><td>data[i].quantity</td><td>data[i].amount</td><td><input type="checkbox" class="add_checkbox" name="layout" id="add_checkbox" value="1" checked></td></tr>');

                    }
                }
                if(txt != ""){

                    $("#table").append(txt).removeClass("hidden");
                }
            }
        }
    },
    error: function(jqXHR, textStatus, errorThrown){
        alert('error: ' + textStatus + ': ' + errorThrown);
    }
});
return false;
 });
</script>  

Here I want to pass the values of json_data in to the table i had written and how can we pass that here am getting error like undefined.

Here is my controller

public function ajax_data()
 {
  $array = array("agent_name" => $_POST['agent_name'],"number"=>$_POST['number'],"type"=>$_POST['type'],"quantity"=>$_POST['quantity'],"date"=>$_POST['date'],"commision"=>$_POST['commision'],"profit"=>$_POST['profit'],"agent_amount"=>$_POST['agent_amount'],"user_id"=>$_POST['user_id']);
  $data['json'] = $array;
  echo json_encode($data);
}

here is my json_data which looks like this

{"json":{"agent_name":"admin","number":"444","type":"super","quantity":"4","date":"2018-02-14 15:16:27","commision":"10.00","profit":"40.00","agent_amount":"0.00","user_id":"1"}}
javascriptjqueryjsonajaxcodeigniter-3

Answers

comments powered by Disqus