hello I have a json array when I execute $('tbody').html(data.table_data);
in my ajax I get the following return
[{"id":28,"fname":"tester","lname":"testlast","phone":"00000000","email":"[email protected]","address":"Tester 10","country":"TesterCountry","city":"TesterTown","bday":"2222-02-22","username":"admin","password":"1234","access":"Admin","created_at":"2018-10-13 16:34:22","updated_at":"2018-10-14 12:50:26"},{"id":29,"fname":"tester2","lname":"tester2last","phone":"11000000","email":"[email protected]","address":"Testaria 22","country":"TEsterio","city":"Testeriontiown","bday":"8812-09-08","username":"admin","password":"1234","access":"admin","created_at":"2018-10-14 09:16:50","updated_at":"2018-10-14 12:51:26"}][{"id":28,"fname":"tester","lname":"testlast","phone":"00000000","email":"[email protected]","address":"Tester 10","country":"TesterCountry","city":"TesterTown","bday":"2222-02-22","username":"admin","password":"1234","access":"Admin","created_at":"2018-10-13 16:34:22","updated_at":"2018-10-14 12:50:26"},{"id":29,"fname":"tester2","lname":"tester2last","phone":"11000000","email":"[email protected]","address":"Testaria 22","country":"TEsterio","city":"Testeriontiown","bday":"8812-09-08","username":"admin","password":"1234","access":"admin","created_at":"2018-10-14 09:16:50","updated_at":"2018-10-14 12:51:26"}]
and with data.total_data I get the number from all the total records I have
ok So whenever I try to output this data *id:28,fname:tester etc I get undefined.
the code is the following .
function fetch_customer_data(query = '')
{
$.ajax({
url:"{{ route('index.action') }}",
method:'GET',
data:{query:query},
dataType:'json',
success:function(data)
{
var client_data = '';
$.each(data,function (key,value) {
client_data += '<tr>';
client_data += '<td>' +value.id +'</td>';
client_data += '<td>' +value.fname+'</td>';
client_data += '<td>' +value.lname+'</td>';
client_data += '<td>' +value.email+'</td>';
client_data += '<td>' +value.phone+'</td>';
client_data += '<td>' +value.address+'</td>';
client_data += '<td>' +value.country+'</td>';
client_data += '<td>' +value.city+'</td>';
client_data += '<td>' +value.bday+'</td>';
client_data += '<td>' +value.username+'</td>';
client_data += '</tr>';
})
$('tbody').html(cleint_data);
$('#total_records').text(data.total_data);
}
})
}
Edit: I dont know how to provide you with an actual response so I do the next best thing. Ill show you how I generate it. maybe that's where my problem is
function action(Request $request)
{
if ($request->ajax()) {
$output = '';
$query = $request->get('query');
if ($query != '') {
$data = DB::table('users')->
where('fname', 'like', '%' . $query . '%')
->orWhere('lname', 'like', '%' . $query . '%')
->orWhere('email', 'like', '%' . $query . '%')
->orWhere('phone', 'like', '%' . $query . '%')
->orWhere('address', 'like', '%' . $query . '%')
->orWhere('country', 'like', '%' . $query . '%')
->orWhere('city', 'like', '%' . $query . '%')
->orWhere('bday', 'like', '%' . $query . '%')
->orWhere('username', 'like', '%' . $query . '%')
->orWhere('access', 'like', '%' . $query . '%')
->orderBy('id', 'desc')
->get();
} else {
$data = DB::table('users')
->orderBy('id', 'asc')
->get();
}
$total_row = $data->count();
if ($total_row > 0) {
foreach ($data as $row) {
$output .= $data;
}
} else {
$output = '
<tr>
<td align="center" colspan="13">No Data Found</td>
</tr>
';
}
$datas = array(
'table_data' => $output,
'total_data' => $total_row
);
echo json_encode($datas);
}
}
edit Here is the json output
{table_data: "[{"id":28,"fname":"tester","lname":"testlast","pho…14 09:16:50","updated_at":"2018-10-14 12:51:26"}]", total_data: 2}
table_data: "[{"id":28,"fname":"tester","lname":"testlast","phone":"00000000","email":"[email protected]","address":"Tester 10","country":"TesterCountry","city":"TesterTown","bday":"2222-02-22","username":"admin","password":"1234","access":"Admin","created_at":"2018-10-13 16:34:22","updated_at":"2018-10-14 12:50:26"},{"id":29,"fname":"tester2","lname":"tester2last","phone":"11000000","email":"[email protected]","address":"Testaria 22","country":"TEsterio","city":"Testeriontiown","bday":"8812-09-08","username":"admin","password":"1234","access":"admin","created_at":"2018-10-14 09:16:50","updated_at":"2018-10-14 12:51:26"}][{"id":28,"fname":"tester","lname":"testlast","phone":"00000000","email":"[email protected]","address":"Tester 10","country":"TesterCountry","city":"TesterTown","bday":"2222-02-22","username":"admin","password":"1234","access":"Admin","created_at":"2018-10-13 16:34:22","updated_at":"2018-10-14 12:50:26"},{"id":29,"fname":"tester2","lname":"tester2last","phone":"11000000","email":"[email protected]","address":"Testaria 22","country":"TEsterio","city":"Testeriontiown","bday":"8812-09-08","username":"admin","password":"1234","access":"admin","created_at":"2018-10-14 09:16:50","updated_at":"2018-10-14 12:51:26"}]"
total_data: 2
value['id']. You have a typo in$('tbody').html(cleint_data);Can you confirm that this is not in your actual code?total_datashown in the json so my guess is you aren't showing us the full response structure and that the array shown is also a property of a higher level object we can't see$.each(data.table_data.....and update question with full structure of the response and fix typo forcleint_data