Passing data from controller to view using ajax in codeigniter - php

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);

Related

Get All name From Table1 Where id (Separated by Comma) from Table2 - Ignited Datatables Server Side Using Codeigniter

I get stuck with my code. If you know about it, please respond to this question, cause I very need your help.
I have 2 tables (proposal and surveyor).
Proposal field :
proposal_id, proposal_name, surveyor_proposal
Example row : ('1', 'this is proposal name', '3,18,22')
As you can see, this is have 3 surveyor id separated by comma)
Surveyor field :
surveyor_id, surveyor_name
Example row :
('3', 'Randy')
('18', 'Bella')
('22', 'Zidan')
! here it is, I want to print out Randi, Bella, and Zidan in the view table
Model.php :
function get_all_datatables($x) {
$this->datatables->select('proposal_name, surveyor_name');
$this->datatables->from('proposal');
$this->datatables->join('surveyor','surveyor_id = surveyor_proposal','left');
return $this->datatables->generate();
}
Controller.php :
function get_data_json($x) { //get product data and encode to be JSON object
header('Content-Type: application/json');
echo $this->m_bl->get_all_datatables($x);
}
View.php :
$("#table").dataTable({
initComplete: function() {
var api = this.api();
$('#table_filter input')
.off('.DT')
.on('input.DT', function() {
api.search(this.value).draw();
});
},
oLanguage: {
sProcessing: "loading..."
},
processing: true,
serverSide: true,
ajax: {"url": "<?= base_url('dashboard/bl/get_data_json/'.$uri); ?>", "type": "POST"},
{"data": "proposal_name"},
{"data": "surveyor_name"},
rowCallback: function(row, data, iDisplayIndex) {
var info = this.fnPagingInfo();
var page = info.iPage;
var length = info.iLength;
$('td:eq(0)', row).html();
}
});
Update!
I've been add 1 table junction, the fields is :
proposal_id_junction, surveyor_id_junction
Example Value :
('1','3') --> as Randy
('1','18') --> as Bella
('1','22') --> as Zidan
And i update my table join :
$this->datatables->join('junction','proposal_id_junction = proposal_id','left');
$this->datatables->join('surveyor','surveyor_id = surveyor_id_junction','left');
But, thats showing same proposal and different surveyor name, like this :
'this is proposal name','Randy'
'this is proposal name','Bella'
'this is proposal name','Zidan'
I want to thats view like this :
'this is proposal name','Randy, Bella, Zidan'
Please help.

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
});

Datatables - sorting by and pagination

I'm fresh, but I'm learning with the help of people like you :). I need to do a table on my site that will display sorted data by type and pagination. I thought to use this solution: https://legacy.datatables.net/examples/data_sources/server_side.html
But honestly, I do not know how to do this. Can someone give an example along with html how to do it? Or suggest a different solution?
First of you need to import some of the necessary files:
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
CSS :
<link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet">
Then using the id attribute of the table you have created use the following code :
var table = $('#myTable').DataTable(); // this creates a table object of the DataTable class
// myTable is the id of the table you have created
table.clear().draw();
table.destroy();
$.ajax({
url: 'abc.php',
type: 'POST',
data: {value:value},//value you want to send to backend
dataType: 'json',
success:function(result){
$('#myTable').DataTable( {
"pageLength": 10, // does pagination providing 10 records per page
"destroy": true,
"language": {
"zeroRecords": "No Data Found",
"infoEmpty": "No Data Found",
},
"ajax": "objects.txt",
// Data from backend is sent to objects.txt file in JSON format
"columns": [
{ "data": "Key value from backend for 1st column in table" },
{ "data": "Key value from backend for 2nd column in table" },
{ "data": "Key value from backend for 3rd column in table" },
{ "data": "Key value from backend for 4th column in table"},
{ "data": "Key value from backend for 5th column in table" },
{ "data": "Key value from backend for 6th column in table" }
]
});
},
error: function (xhr, ajaxOptions, thrownError) {
alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
alert("responseText: "+xhr.responseText);
}
});
You could import more datatable javascripts that can provide more functionality such as conversion of table data to pdf or export as an excel file using button datatable javascripts. You can find more related information here : https://cdn.datatables.net/

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?

Can't figure out how to parse this data

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.

Categories