Jquery autocomplete with post data - php

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

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

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.

jquery click happens too fast to refresh results from database?

I'm making a simple shoutbox and when someone posts a message the message is added to the database just fine. I made a refresh button which populates all the messages in the text area.
I am trying to call the .click() on the refresh button after the person enters the message but I think it happens too fast and doesn't catch the new message in the database. I works if I add an alert to give it more time. Is there a better way?
$("#shoutBtn").click(function(event){
event.preventDefault();
$.post("includes/shoutBoxPost.php", {"message": $("#messageBox").val(),
"name": $("#userNameWelcome").html()});
$("#messageBox").val("");
$("#shoutRefresh").click();
});
I changed my code to the long hand ajax way and it works thanks for answers folks
$.ajax({
type: 'POST',
data: {"message": $("#messageBox").val(),
"name": $("#userNameWelcome").html()},
url : 'includes/shoutBoxPost.php',
success : function(data) {
$("#messageBox").val("");
$("#shoutRefresh").click();
}
});
See the documentation for jQuery .post. You can define a success callback that will only fire once the post request successfully returns.
jQuery.post( url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] )
$.post("includes/shoutBoxPost.php",
{"message": $("#messageBox").val(), "name": $("#userNameWelcome").html()},
function( data, textStatus, jqXHR ) {
$("#shoutRefresh").click();
}
);
you can add call back function to your post
$.post( "includes/shoutBoxPost.php",{"message": $("#messageBox").val(), "name": $("#userNameWelcome").html()})
.done(function( data ) {
$("#messageBox").val("");
$("#shoutRefresh").click();
});
You need to wait for your post to finish and send a success message back (via JSON), then do the refresh action.

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