Autocomplete Jquery Json - javascript

I'm trying to use an external Json as source for the autocomplete Jquery UI plugin : http://jqueryui.com/autocomplete/
This code works fine:
var availableTags = ["aberdeen","aberystwyth","aberystwyth juniors"]
$( "#enter-your-parkrun" ).autocomplete({
open: function(e) {
var results = $.ui.autocomplete.filter(availableTags, e.term);
response(results);
valid = false;
},
select: function(e){
valid = true;
},
close: function(e){
if (!valid) $(this).val('');
},
source: availableTags,
});
$("#enter-your-parkrun").change(function(){
if (availableTags.indexOf($(this).val()) == -1){
$(this).val("");
}
});
but as I have almost 800 values, I need to use an external source. I've tried different things but I can't find a way to make it work:
$( "#enter-your-parkrun" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "http://tribesports.com/pages/parkrun-event-list",
dataType: "jsonp",
data: {
q: request.term
},
success: function( data ) {
response( data );
}
});
},
minLength: 2,
delay: 400,
});
I'm not sure what q: request.term should be and if data should be parsed? I also need to add validation to make sure that only the values in the list can be accepted, it works on my first example, not too sure how to transfer this to my second code.
Thanks

request.term is what the user has typed so far, and should be passed to the server as a parameter in the AJAX call so that the server can filter the results.
In the .ajax call, dataType is the kind of data you're expecting back from the server. You have not shown it, but most probably is json, not jsonp. If it's JSON jQuery parses it automatically.
As to sending the term to the server,
if you use the GET method (which is the default), you have to include it in the query string, like this:
url: 'http://tribesports.com/pages/parkrun-event-list?term='+request.term
if you use other method (usually POST) you have to specify the method option in the jquery ajax call options, and also specify the contentType, which shuld be JSON: contentType: "application/json", and send the data in the requiredn format
As you can see this all depends on what's on the server.

Related

Pick the value from a text box id to PHP

Iam getting a value to the text box id using AJAX which is working fine.
<input type='text' id='selectuser_id' />
Javascript
$( "#customers" ).autocomplete({
source: function( request, response ) {
var countrycode = '<?php echo $agencyid; ?>';
$.ajax({
url: "http://localhost:8000/core/country/fetch_customers.php",
type: 'post',
dataType: "json",
data: {
search: request.term
},
success: function( data ) {
response( data );
}
});
},
select: function (event, ui) {
$('#customers').val(ui.item.label); // display the selected text
$('#selectuser_id').val(ui.item.value); // save selected id to input
var customerid = $("#selectuser_id").val(); //equals $q6[$t2] exactly
return false;
}
});
Now how will i pick this value to my PHP
<?php echo $selectuser_id; ?>
I tried many ways but not getting the result. Can someone help me on this?
Since you don't show us where you hold the data of the input field i will give you an example.
Javascript side (since you are also using jQuery) you can get the value of the input field by id like:
let myValue = $( "#selectuser_id" ).val();
Then inside your ajax call you can include your data just like you did for the search:
data: {
search: request.term,
myValue: myValue
}
In your fetch_customers.php file you should be able to access your post data like:
$_POST['myValue'];

how to use ajax in jQuery in javascript

I want to post javascript variable to another php file and I search using Ajax may help.I want to get back the data on another php file by "post" method.But I can't use $.ajax({}) directly inside javascript:
function input() {
$.ajax({
url : 'update_service.php',
type : 'POST',
data : {
name: name,
},
success : function(response) {
alert(response);
}
});
The error message said "unexpected function ajax()". How to fix it?
JQuery Version
$.post( "update_service.php", function( data ) {
alert( "Data Loaded: " + data );
});

Sending the body of a page to another server?

Is it possible to send the body of a page (i.e. all of the HTML code/DOM) from a page on one server to another and then receive a response? The method used doesn't really matter, but I do have access to jQuery functions if that helps.
You could do this by sending...
$("html").html();
...in an ajax call:
var data = $("html").html();
$.ajax({
url: "/echo/json/",
data: data,
type: "POST"
}).done(function(res) {
console.log(res);
});
Working jsFiddle: http://jsfiddle.net/brandonscript/p5zEW/
(See console for body output).
Something like this should work:
$.get( "somePage.html",
function( pageData ) {
$.post("targetPage.php", pageData, function( data ) {
alert("Done!");
},
"html"
);
});

Jquery Autocomplete & Codeigniter

Just after a little help if possible,
Im using the Codeigniter framework and i have managed to get jQuery autocomplete working, with information from a mysql database.
But have come a little bit stuck on getting the results of the auto complete to show other information in text field.
Im just trying to get my head around Jquery at the moment but have very limited knowledge of it so any help or pointers would be a great help.
My JS code
$(document).ready(function() {
$(function() {
$( "#Name" ).autocomplete({
source: function(request, response) {
$.ajax({ url: "<?php echo site_url('autocomplete/namesuggestions'); ?>",
data: { term: $("#Name").val()},
dataType: "json",
type: "POST",
success: function(data){
response(data);
}
});
},
minLength: 1
});
});
});
With Two input boxes with ids of Name and another of Address
Just to make things a bit more confusing i can get the address to appear in the Name and address to appear in the Name text box when it auto completes ??
Any Help would be greatly appreciated
You have to map the response for ajax responses and for fill other input box onselect you can use select: option in autocomplete like this
var source_name = {
source:function(request, response){
$.getJSON('your url and parameter as get',function(data){
if(data != null){
response($.map(data, function (item) {
return {
value: item.name,
addr: item.addr
}
}));
}
});
},
select: function(event, ui){
$('#addr').val(ui.item.addr);
}
};
$('#Name').autocomplete( source_name);
This will work fine if you are using jquery ui and if you return your json properly as to get values. Hope This helps you.

Getting JSON response from the server but can't display as HTML with Jquery

I'm building a mobile app and i build a PHP API that renders data from a mysql db and encode it as JSON
i call this api with Jquery ajax to get to display the records from the json output as JSONP and render it in my document in the success function.
so far i get the JSON correctly when i do the request via $.ajax and i can see the data in the Response in my firebug but in the other side the ajax function fires the ERROR not the Success.
i have a demo code in here :jsfidle
this is my $.ajax Call:
$(document).on("pageinit","#myPage", function() {
$("#autocomplete").on("listviewbeforefilter", function ( e, data ) {
var $ul = $( this ),
$input = $( data.input ),
value = $input.val(),
html = "";
$ul.html( "" );
var dataString = 'keyword='+value;
if (value.length > 2 ) {
$.mobile.loading("show");
$.ajax({
type: "GET",
url: "http://example.com/search.php",
dataType: "jsonp",
jsonpCallback: 'jsonpCallback',
cache:true,
data: dataString,
success: function(data) {
$.mobile.loading("hide");
alert(data);
}
})
}
});
});
if you check the net panel you 'll find it successful and there is data coming.
Kindly Advise.
Thanks A lot
Your response is not including the callback jsonpCallback() in the response, the response I'm seeing is simply
({"name": ... })

Categories

Resources