I try to pass ids as array via ajax but when I dump out the get variable on server side with PHP I do not get any values, the array is empty. Debugging with firebug I see that the array is getting passed but [] is encoded ids%5B%5D
my javascript
function update_category(selected) {
$.ajax({
url: '/admin/?controller=products&action=update_category',
type: 'GET',
dataType: "application/JSON",
data: {
ids: cat_ids,
s_category: selected
},
success: function(data) {
addAlert('alert-'+data, data);
},
error: function(data) {
addAlert('alert-'+data.responseText, data.responseText);
}
});
}
var_dump $_GET['ids'] null dumping the hole $_GET I get ["ids%5B%5D"]
I do not understand why the array is getting encoded on submit in linux environment
Under ubuntu 12.10 PHP 5.3.10 Firebug XHR->PARAMS
action update_category
controller products
ids%5B%5D 1403172219
ids%5B%5D 1530542001
s_category 1
how to pass properly the array?
Since you are already passing GET values int he URL here:
/admin/?controller=products&action=update_category
Then the information in data probably never passes.
Try this instead:
function update_category(selected) {
$.ajax({
url: '/admin/',
type: 'GET',
dataType: "application/JSON",
data: {
ids: cat_ids,
s_category: selected,
controller: 'products',
action: 'update_category'
},
success: function(data) {
addAlert('alert-'+data, data);
},
error: function(data) {
addAlert('alert-'+data.responseText, data.responseText);
}
});
}
Related
Sometimes i get report that in jQuery Ajax query $_POST is empty.
For example Google Chrome on OS X or Windows 7 Firefox sometimes the post is empty sometime it isn't.
I can't figure out what seems to be the problem
jQuery.ajax({
url: "ajax.php",
method: "POST",
data: {
jqueryAjaxPostData: JSON.stringify({
name: "gen",
id: 1
})
},
processData: true,
dataType: "json",
failure: function ()
{
alert("Ajax failure")
}
});
EDITED : There is PHP code
$post = Variable::toArray(json_decode($this->Http->post("jqueryAjaxPostData")));
After days (and a bit of spamming here and there), I did get CORS to work for my CodeIgniter applications. However, POST variables are empty in the PHP side in live server. the whole $_POST array is empty for some reason. Here is my ajax code:
var postForm = {'e':'sammy'}; //data to process
$.ajax({
type: "POST",
url: "http://www.abc.ca/types/add",
data: postForm,
dataType : "json",
cache: "false",
contentType: "application/json",
success: function (result) {
//result here is blank
alert(result);
},
fail: function (result){
alert(result);
}
});
and the called php function (without the whole controller class):
function add() {
//Add new a biz type
file_put_contents('trial.txt',implode(" | ",$_POST));
echo $this->input->post('e');
}
Am I missing something? the response from server are ok. but still the file created contains no data and echo prints empty. Any tips please? I have already tried method:'POST' instead of type:'POST'. Get variables pass away successfully.
Is this CI issue or Ajax because it is constant across ff,chrome and Opera
Try This Ajax :
$.ajax({
type: "POST",
url: "http://www.abc.ca/types/add",
data: postForm,
cache: "false",
success: function (result) {
//result here is blank
alert(result);
},
fail: function (result){
alert(result);
}
});
I am trying to get my search bar working, however I am having issues with an ajax post.
For whatever reason, none of the data is being appended to the URL. I have tried various things with no success. I am attempting to send the data to the same page (index.php).
Here is my jquery:
$(function(){
$(document).on({
click: function () {
var tables = document.getElementsByTagName("TABLE");
for (var i = tables.length-1; i >= 0; i-= 1) {
if (tables[i]) tables[i].parentNode.removeChild(tables[i]);
}
var text = $('#searchBar').val();
var postData = JSON.stringify({ searchTerm: text });
$.ajax({
type: 'POST',
url: 'index.php',
dataType: 'json',
data: postData,
success: function() {
alert("Test");
}
});
}
}, "#searchButton");
});
And here is the php which I have with index.php:
<?php
require('course.php');
if(isset($_POST['searchTerm'])) {
echo $_POST['searchTerm'];
}
?>
No matter what I try, I am unable to get anything to post. I have checked the network tab in chrome, and I'm not seeing anything that indicates it's working correctly.
Any ideas?
EDIT:
I've changed my code to this, and it seems I'm getting closer:
$(document).on({
click: function () {
$("TABLE").remove()
var text = $('#searchBar').val();
$.ajax({
type: 'GET',
url: 'index.php',
dataType: 'text',
data: { searchTerm: text },
success: function() {
alert("Test");
}
});
}
}, "#searchButton");
And:
<?php
require('course.php');
if(isset($_GET['searchTerm'])) {
echo $_GET['searchTerm'];
}
?>
Now I am getting ?searchTerm=theTextIEnter as expected, however it's still not being echoed in index.php.
Do not use JSON.stringify() to convert object to string. data passed to $.ajax must be an object and not JSON.
For whatever reason, none of the data is being appended to the URL.
You are making a POST request. POST data is sent in the request body, not in the query string component of the URL.
If you change it to a GET request (and inspect it in the correct place, i.e. the Network tab of your browser's developer tools and not the address bar for the browser) then you would see the data in the query string.
This will work for you use data: { postData } on place of data:postData and you will receive your data in $_POST['postData']
$(function(){
$(document).on({
click: function () {
var tables = document.getElementsByTagName("TABLE");
for (var i = tables.length-1; i >= 0; i-= 1) {
if (tables[i]) tables[i].parentNode.removeChild(tables[i]);
}
var text = $('#searchBar').val();
var postData = JSON.stringify({ 'searchTerm' : text });
$.ajax({
type: 'POST',
url: 'index.php',
dataType: 'json',
data: { postData },
success: function(data) {
alert(data.searchTerm);
}
});
}
}, "#searchButton");
});
In index.php
<?php
if(isset($_POST['postData'])) {
echo $_POST['postData'];
die;
}
?>
If you want to send data via the URL you have to use a GET request. To do this, change the type of the request to GET and give the object directly to the data parameter in your jQuery, and use $_GET instead of $_POST in your PHP.
Finally note that you're not returning JSON - you're returning text. If you want to return JSON, use json_encode and get the value in the parameter of the success handler function.
Try this:
$(document).on({
click: function () {
$('table').remove();
$.ajax({
type: 'GET',
url: 'index.php',
dataType: 'json',
data: { searchTerm: $('#searchBar').val() },
success: function(response) {
console.log(response.searchTerm);
}
});
}
}, "#searchButton");
<?php
require('course.php');
if(isset($_GET['searchTerm'])) {
echo json_encode(array('searchTerm' => $_GET['searchTerm']));
}
?>
Remove dataType: 'json', from your AJAX. That is all.
Your response type is not JSON, yet by setting dataType: 'json' you're implying that it is. So when no JSON is detected in the response, nothing gets sent back to the method handler. By removing dataType it allows the API to make an educated decision on what the response type is going to be, based on the data you're returning in the response. Otherwise, you can set dataType to 'text' or 'html' and it will work.
From the manual:
dataType: The type of data that you're expecting back from the server.
This is NOT the type of data you're sending/posting, it's what you're expecting back. And in your index.php file you're not sending back any JSON. This is why the success() method is not satisfying. Always set the dataType to the type of data you're expecting back in the response.
With POST Request:
Please comment below line in your code:
//var postData = JSON.stringify({ searchTerm: text });
And use below ajax code to get the post-data:
$.ajax({
type: 'POST',
url: 'index.php',
dataType: 'json',
data: { searchTerm: text },
success: function() {
alert("Test");
}
});
With GET Request:
You can convert your data to query string parameters and pass them along to the server that way.
$.ajax({
type: 'GET',
url: 'index.php?searchTerm='+text,
dataType: 'json',
success: function(response) {
alert(response);
}
});
In response, You can get the data with alert, so you may get idea about the same.
I've used ajax quite a bit to send data off to a php page and get something back, but I'm not sure how to determine what specifically gets sent back. I understand what most of the fields here do, but I have a few questions as well.
My current code looks like this:
$.ajax({
url: "search.php",
type: "post",
dataType: "json",
data: {partNumber: q , newType:newType},
success: function(data) {
console.log(data);
}
});
One thing I don't fully understand is how the success: line works. When I say success: function(data), it's my understanding that I'm saying "upon success (of something? I'm not sure what), the information that is returned to this ajax function will be called data," and then inside the brackets, I'm choosing to console.log that data.
Please correct me if I'm wrong in that assumption.
Ultimately, my php page will do a ton of work and return an array with data in it. If the last line of my php code is:
$myArray = array("bob","dan","carl","mike");
how do I choose for that to be returned to ajax? The php page is working fine, but right now my console.log(data) line of code is not returning anything.
You are not supposed to return anything from the php page called by your ajax, you're supposed to echo it (i.e. write it to the response).
echo json_encode($myArray);
And if what you're echoing is not JSon, then take out dataType: "json". The success function means that the request was successful, i.e. a response was received back from the server with status 200. There is also an error method you can use for when you don't get a successful response back.
success: function(data) {
console.log(data);
},
error: function(data) {
console.log("Error: "+data);
}
1) Your on "success" is basically saying when there is not an error that occurs.
Here is testing error
$.ajax({
url: 'search.php',
success: function(){
alert('success');
},
error: function(){
alert('failure');
}
});
2) For your "data" what you echo on the php side gets returned into the variable defined in your
function(results) {
for example if you want to return an array you may want to return "echo" json
your array should include a key and a value
$myArray = array("key" => "value");
echo json_encode($myArray);
and on the jquery side parse the json object returned.
function(data) {
var obj = jQuery.parseJSON(data);
alert(obj.key);
3) Pass JSON Objects
var JSONObject= {"partNumber":q, "newType":newType};
var jsonData = JSON.parse( JSONObject );
var request = $.ajax({
url: "search.php",
type: "POST",
data: jsonData,
dataType: "json"
});
The following code sends some data to a PHP file. That PHP file encodes to a JSON object and echoes three variables error, success, and action.
However, neither the success function here, nor the error function appear to be returning that JSON object.
How would one get the values for these three keys back.
$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
url: ua['url'],
data: { sconnect: 'email', email : $('#semail').val(), hack:86 },
dataType: 'json',
success: function (data) {
alert(data['success']); alert(data['error']); alert(data['action']);
},
error: function(data){
alert(data['success']); alert(data['error']); alert(data['action']);
}
});