Can't figure out how to parse this data - php

The response I'm getting back from PHP using an AJAX call:
$.ajax({
url: 'getit.php',
type: 'POST',
dataType: 'json',
data: {id_token: my_token},
success: function(data) {
//stuff
}
});
gives me something like this when I console.log it:
email: "me#example.co"
email_verified: true
first_name: "Bob"
permissions: Object
client_1001: Object
client_id: "121434"
role: "full"
table_name: "5tyyd"
client_1002: Object
client_id: "45638"
role: "full"
table_name: "df823"
If I do something like this:
$('.last_name').text(data.first_name);
and
<div class="last_name"></div>
This gives me back Bob just as expected.
What I need to get is a list of permissions (e.g., client_1001) and the pieces of data under each permission. I can't figure out how to do though.
This is where I'm stuck:
$('.last_name').text(data.permissions.WHATGOESHERE?);
I need to fill in the WHATGOESHERE? part. It's basically an object in an object, but I'm not sure how to parse it.

As you're using jQuery, give this a try :
$.each(data.permissions, function (index, value) {
console.log(index); // "client_0001"
console.log(value.table_name); // "5tyyd"
});
The idea is to iterate through the response, as if you were using foreach() in PHP.

Related

Passing data from controller to view using ajax in codeigniter

I want to pass value from my controller's function to be processed in view as a chart. But I had a problem with passing the value data.
I've tried to pass the data but it returns null.
Here it is the data that i want to pass through view :
0: {total: "12", person: "sakti.sakti#yaho.com", activity: "Request Config Files"} activity: "Request Config Files" person: "sakti.sakti#yaho.com" total: "12"
and this is my view:
$("#btn_ixt_filter").click(function(){
// get_ixt_report("<?php echo base_url();?>index.php/Dashboard/ict_report/" + star_date + "-" + end_date);
$.ajax({
url:"<?php echo base_url(); ?>Dashboard/ict_report/" + star_date +"-"+ end_date,
method: "POST",
dataType : "json",
data: {
'category' : $("#categoryField").val(),
'selection': $("#selectionField").val(),
'counter' : $("#counterField").val(),
'group_by' : $("#groupbyField").val(),
'userid' : $("#useridField").val(),
'role' : $("#roleField").val(),
'customer' : $("#customerField").val(),
'project' : $("#projectField").val(),
'combiner' : $("#combinerField").val(),
},
success:function(data){
$('#result').html(data);
$total.html(total_ticket);
$activity.html(activity);
}
})
event.preventDefault();
The question is how to get these data, total, person, and activity. Thanks
url:"<?php echo base_url(); ?>Dashboard/ict_report/" + star_date +"-"+ end_date,
so at your ict_report method .. you have all post data and 2 parameter as start date and end date .... with help of it get data from db ... and the resulted array ... merge all array into one ... and echo the result as json_encoded ...
and at you js ... parse you json_encoded data...
var data = JSON.parse( data );
console.log(data.arrayKey);

How to make Typeahead.js search a specific DB (of multiple) in Laravel 5.7

I have 2 databases in my Laravel 5.7 app, and I would like to add a typeahead functionality with one of them.
It is the first time I use it, so I don't really know how to do it.
I'm following the instructions here: https://itsolutionstuff.com/post/laravel-57-autocomplete-search-from-database-using-typeahead-jsexample.html
I have used typeahead in my application. Hope this would help you.
$(document).ready(function(){
$.ajax(
{
type : 'GET',
url: base_url+"/getrelateduser",
success: function (data)
{
// Defining the local dataset
var relateduser = data;
// Constructing the suggestion engine
var relateduser = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: relateduser
});
$('.sendleads').typeahead({
hint: true,
highlight: true, /* Enable substring highlighting */
minLength: 3, /* Specify minimum characters required for showing result */
},
{
name: 'relateduser',
source: relateduser
});
}
});
});
Here the url "/getrelateduser" will return all the emails of users in the users table. Let me know if you have more queries.

Jquery autocomplete with post data

I have a script with autocomplete, get some data from an external source according to searched term.
I can output the json in the console but I'm struggling to pass it to the response, how do I do that?
$('#test').autocomplete({
source: function(request,response){
$.post('/schoollookup', {
query: request.term
}, function(data){
}, 'json'
);
},
minLength: 2
});
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
This is the syntax for post request. where
url : A string containing the URL to which the request is sent.
data : A plain object or string that is sent to the server with the request.
success : callback function
#Sumesh
$.post('/schoollookup', {
should be working the same, the difficulty that I have is to get response
Thank you for your answer r007ed, the issue was that it was not returning an array.
So the final code for this is :
$('#test').autocomplete({
source: function(request,response){
$.post('/schoollookup',{query: request.term}, response, 'json');
},
minLength: 2
});

Get Jquery and PHP response from second to second

I have a page in PHP that does a query in the database at the end of the processing I show the amount of updated records. This process takes a while, in my Jquery I just show the total of affected records, how could I change my Jquery to show from time to time, in case every 1 second it shows the user the amount of records.
var form_data = new FormData();
form_data.append('id', idSel);
$.ajax({
url: 'post.php',
type: 'POST',
data: form_data,
cache: false,
contentType: false,
processData: false,
beforeSend: function () {
$('.resp').html("<img src='images/spin.gif' alt='Notificação' height='70' width='70'/> ");
},
success: function(response) {
ATTENTION I can not keep calling the PHP page several times, I need to call once and listen to it or receive data from it!
Is it possible for teachers?

Flot points don't connect

I'm trying to make a real time graph that updates every X seconds and then graphs it as time goes on. The problem I'm having is that when it plots the points, the points aren't connected. Here's what I have so far:
var r = [];
function fetchData() {
var options = {
lines: { show: true },
points: { show: true },
xaxis: { show: false }
};
function doSomething(series)
{
r.push(series);
$.plot($("#xx"), r, options);
}
$.ajax({
url: 'playersonline.php',
method: 'GET',
dataType: 'json',
success: doSomething
});
setTimeout(fetchData, 3000);
}
fetchData();
This is what the output looks like currently.
some pic http://screensnapr.com/e/ra6q70.png
It looks like each of your points is a separate series. For them to be connected, you need to put all the points in a single array, and give that to Flot as one series.
If you look at Flot's examples, there's one that demonstrates real-time updates of a single series.

Categories