I got a strange situation. Below are select2 code.
$("#tags").select2({
multiple: true,
placeholder: "Please enter tags",
tokenSeparators: [',', ' '],
initSelection : function (element, callback) {
var data = [];
$(element.val().split(",")).each(function () {
data.push({id: this, text: this});
});
callback(data);
},
ajax: {
multiple: true,
url: "fetch.php",
dataType: 'json',
type: "POST",
data: function(term, page) {
return {
term: term
};
},
results: function(data, page) {
return {results: data};
},
},
And here is php file fetch.php
$search = strip_tags(trim($_GET['term']));
Problem is $search didn't have any value. I checked var_dump($_GET) but no value is there. It is showing null.
I checked in Firebug and found post is working fine, but for some reason it is not showing in $_GET.
Screen shot attached below.
You're POSTing the variables, not sending them as querystring.
Var_dump $_POST and you'll find them.
Related
I used ajax.reload(), but it is not working.
I try the other method to refresh the data but how can i stay on the current pagination table after data been edited.
$.ajax({
type: 'GET',
url: '/test',
dataType: 'HTML',
success: function (html) {
$('#group').html(html);
},
error: function (data) {
console.log('Error:', data);
}
});
I able to refresh the page if using above method but it will always return back to first pagination table. Anyone got idea how to make it how can i stay on the current pagination table after data been edited?
First, you need to get your current page (example: 3).
When you call the function to refresh the content, you need to pass your current page as parameter. For example:
var myCurrentPage = 3;
$.ajax({
type: 'GET',
url: '/test',
dataType: 'HTML',
data: {page: myCurrentPage}
success: function (html) {
$('#group').html(html);
},
error: function (data) {
console.log('Error:', data);
}
});
The var could be dynamic using input hidden or some element attribute
I hope that you have understood the logic. Tell me if not.
Im using this for stay on Page (before reload). "bStateSave" : true,
I hope this will answer your reload page.
$(document).ready(function () {
$('#users').DataTable({
"bStateSave" : true,
// "searching" : false,
"columns": [
{"data": "id"},
{"data": "name"},
{"data": "phone"},
{"data": "fax"},
],
"processing": true,
"serverSide": true,
"ajax": {
url: 'demo.php',
type: 'POST'
}
});
});
I am new with laravel, I am having an issue when posting my data to controller in laravel while using ajax. My view contains a select box which is populated from database when a user select a value from select box i am trying to call ajax which will return details of salary of the user.The issue is it gives me 404 not found error.
The Code for the controller file is shown below.
this function is defined inside a controller
public function getPostSalary()
{
echo "ajax called";
return 'true';
}
The Routes file is
Route::post('company/salary-user', 'CompanyController#getPostSalary');
this is my ajax code from where the controller is being calleD
<script>
$(document).ready(function() {
$('#employee').change(function () {
var values = $('#employee').val();
if (values == '') {
$.pnotify({
title: 'Message',
text: 'Please Select a User.',
type: 'error',
delay: 3000
});
return false;
}
var csrf = $('#csrf').val();
$.ajax({
url: '{!! URL::to("company/salary-user")!!}',
type: "POST",
data: { user_id: values, _token: csrf },
cache: false,
beforeSend: function () {
},
success: function (data) {
console.log(data);
$('#loader').hide();
}
});
});
});
</script>
Can someone help me to figure out what is the issue and what should be done to call my function.
Single quotes is for string so your url is not generating as you expecting. Use like this
url : "{!! URL::to('company/salary-user')!!}",
You can use direct url like
$.ajax({
url:'company/salary-user',
type: "POST",
data: { user_id: values, _token: csrf },
cache: false,
beforeSend: function () {
},
success: function (data) {
console.log(data);
$('#loader').hide();
}
});
replace this line:
{!! URL::to("company/salary-user")!!}
by :
{{ url('/company/salary-user') }}
or
{{ route('/company/salary-user') }}
Just downloaded a select2 files from github, the latest release 4.0.3, and i want to populate the options based on what i'm typing (ajax request)
heres the code
$('#usernetwork').select2({
ajax: {
url: 'someurl.php',
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term, // search term
page: params.page
};
},
processResults: function (data, page) {
return data.items;
},
cache: false
},
minimumInputLength: 1
});
the url (someurl.php) shows this json result
[{"id":"1","text":"ClickWeb"},{"id":"2","text":"ClickWeb 1"},{"id":"3","text":"ClickWeb 2"},{"id":"4","text":"ClickWeb 3"}]
and finally the select field is this
<select name="usernetwork" id="usernetwork" class="form-control">
</select>
this is the very first time i try this for now im getting nothing as query (for test purposes)
The results are not showing up, and i have no idea on how to fix this, any tips?
Try my answer the only thing you need to change is your return statement inside processResults callback like this
return {
results: data
};
Here is full code :
$('#usernetwork').select2({
ajax: {
url: 'someurl.php',
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term, // search term
page: params.page
};
},
processResults: function (data, page) {
return {
results: data
};
},
cache: false
},
minimumInputLength: 1
});
I post data to sa.php via AJAX and on it I use $_POST['data'] to grab the post and echo it. Now when I hit F12 to see the transfer it shows success. The $_POST['data'] shows up on the sa.php but when you look at the screen no text shows.
retrieve code:
Now here is my AJAX code:
$.ajax({
type: "POST",
data: {"data": data},
dataType: "text",
success: function(data){
console.log(data);
},
statusCode: {
404: function() {
alert( "page not found" );
}
},
complete: function (data) {
console.info(data);
}
});
});
I'm trying to echo the $_POST['data'] but it won't display in html.
As most comments point out you are not updating the HTML but rather just logging to the console....try this... create a div with an id "status"
In your page
<div id="status"></div>
and update your javascript
$.ajax({
type: "POST",
data: {"data": data},
dataType: "text",
success: function(data){
$('#status').html(data); //puts the response in a div with id status
console.log(data);
},
statusCode: {
404: function() {
alert( "page not found" );
}
},
complete: function (data) {
console.info(data);
}
});
});
I presume that you would have a container div for where you want to echo your $_POST['data']? If you do, you can use that div as a selector using jQuery, where you can insert your AJAX data into it. To do that you add to your AJAX success function, something like the following:
success: function(data) {
$('#containerDiv').html(data);
}
Note: .html overwrites whatever is in your selector
Have a look at the first answer of this question, i think this might help achieve what you need:
https://stackoverflow.com/a/2972645/5525510
I want to make autocomlplete for remote data source, I get all data from database and return it as jSon, using console I see that all data has been returned, but the autocomplete doesn't work, also the alert in my code doesn't work, here's my code
$("#cellPhoneNo").autocomplete({
source: function(request, response) {
var param = {
"action": "getCellPhoneNos"
};
$.getJSON("controllers/Customer.controller.php", param, function(result) {
alert('here'); //doesn't alert
// cellPhoneSource=result;
});
},
select: function(event, ui) {
alert('response');
}
});
EDIT
I try to get the source using GET , I make like this
source:function(request,response){
var param= {"action":"getCellPhoneNos"};
$.ajax({
type: "GET",
url: "controllers/Customer.controller.php",
data: param,
success: function(result){
alert('success');
}
});
},
it alerts but autocomplete doesn't work, I try to put the values in a text file and make the file in the url , the autocomplete works!!
Any explanation?!
http://net.tutsplus.com/tutorials/javascript-ajax/how-to-use-the-jquery-ui-autocomplete-widget/
This is a tutorial on using the autocomplete plugin. The response variable in your callback is a function that you can call to add an array of items to the autocomplete list. Parse result and push each item onto an array, then call response(array); If result is already an array, you can call response(result);
I noticed that in the success function you do not return the result from the ajax query, could that be an issue?
source : function(request,response) {
var param= {"action":"getCellPhoneNos"};
var source = 'nothing came back from the server';
$.ajax({
type: "GET",
url: "controllers/Customer.controller.php",
data: param,
datatype: 'json'
success: function(result) {
if(result !== undefined) {
source = result;
}
alert(source);
return source;
}
});
},