Passing jquery array to PHP with AJAX - php

having a bit of an issue with sending a jQuery array to a PHP file. I've looked at similar questions on here, but mine has other elements to the data variable being sent. Here's the code:
var data = 'type='+e+'&offset=' + all_dates_offset + '&filters=' + filters;
$.ajax({
url: "pos_jobs.php",
type: "POST",
cache: false,
data:data,
dataType:"json",
success: function(html){
//Do Something
}
});
For the data, 'e' and 'all_dates_offset' are standard variables, whereas 'filters' is an array. On the PHP side of things, I was hoping I could just use something like $_POST['filters'][0], but that is returning a null value.
Any ideas?
Thanks.

$.ajax({
url: "pos_jobs.php",
type: "POST",
data: {type: e, offset: all_dates_offset, filters: filters},
dataType:"json"
}).done(function(data) {
//do something
});

use below
$.ajax({
url: "pos_jobs.php",
type: "POST",
cache: false,
data:{'type':e,'offset':all_dates_offset,'filters':filters},
dataType:"json",
success: function(html){
//Do Something
}
});

Related

How to pass value to PHP Script

I have code in my html file as below. I am using jQuery Mobile
$.ajax({
type: "GET",
url: "http://localhost/owner_pickup.php",
cache: false,
dataType:'json'
success: function(data)
{
// On success
}
});
owner_pickup.php returns me data by executing query. Now i need to pass a value which i would read in my owner_pickup.php file.
Kindly suggest me how would we pass the value
in your php file:
$value = array(
"dat_1" => "this is data number 1",
"dat_2" => "this is data number 2"
);
echo json_encode($value);
in your jquery finction:
$.ajax({
type: "GET",
url: "http://localhost/owner_pickup.php",
cache: false,
dataType:'json'
success: function(data)
{
var value1 = data.dat_1;
var value2 = data.dat_2;
}
});
please look at this answers:
retrieve multiple values from ajax call
if you don't know how to use JSON please google it.
edit:
pass a value to the php:
$.ajax({
type: "GET",
url: "http://localhost/owner_pickup.php",
cache: false,
data: {
first_value:50,
second_value:55
}
dataType:'json'
success: function(data)
{
var value1 = data.dat_1;
var value2 = data.dat_2;
}
});
in the php:
if(isset($_GET['first_value']))
$first = $_GET['first_value'];
$.ajax({
type: "GET",
url: "http://localhost/owner_pickup.php",
data: {param1: 123, param2: "text value"},
cache: false,
dataType:'json',
success: function(data) { // On success }
});
$.ajax({
type: "GET",
url: "http://localhost/owner_pickup.php",
data:{key1:value1}
cache: false,
dataType:'json'
success: function(data)
{
}
});
In php aaccept it as
<?php
$_REQUEST['key1'];
?>

Sending JSON data using ajax issue

I have the following code:
var arr = {City:'Moscow', Age:25};
$.ajax({
url: "<? echo $this->createUrl('cities/index');?>",
type: "POST",
data: JSON.stringify(arr),
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(data){
alert(data);
}
});
The result is null. In the PHP side I have:
echo json_encode($_POST);
and
print_r($_POST);
But both are giving empty results (checked Firebug also).
You can also set the dataType in Ajax to specify the content type as follows.
var city='city name';
var age=10;
$.ajax({
url: "<? echo $this->createUrl('cities/index');?>",
type: "POST",
data:"City="+city+"&Age="+age,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(data){
alert(data);
}
});
and in cities/index.php you can get this data by follows
if($_POST){
$city=$_POST['City'];
$age=$_POST['Age'];
// and do with $city and $age what you want.
//for return anything thing to `json` use follows we pass $age back to ajax
echo json_encode($age);
}
I guess you don't need to stringyfy the data because data should be PlainObject or String but in your case you can simply write like below
var arr = {City:'Moscow', Age:25};
$.ajax({
url: "<? echo $this->createUrl('cities/index');?>",
type: "POST",
data: arr,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(data){
alert(data);
}
});
as documented in jquery official site https://api.jquery.com/jQuery.ajax/
data
Type: PlainObject or String
Data to be sent to the server. It is converted to a query string, if
not already a string. It's appended to the url for GET-requests. See
processData option to prevent this automatic processing. Object must
be Key/Value pairs. If value is an Array, jQuery serializes multiple
values with same key based on the value of the traditional setting
(described below).
The data option passed to $.ajax() must be either a simple object, that jQuery will transform into a string of the formatkey1=value1&key2=value2.. OR it should be a string of the form key1=value1&key2=value2...
In your case, it can be solved by passing the object itself and letting jQuery do the query string formatting:
$.ajax({
...
data: arr,
...
});
try this
var arr = {City:'Moscow', Age:25};
$.ajax({
url: "<? echo $this->createUrl('cities/index');?>",
type: "POST",
data: arr,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(data){
alert(data);
}
});

how to convert $.post form to $.ajax form?

I am noob at using jquery and ajax. I need to change the form from $.post to $.ajax .
var disqus_config = function() {
this.callbacks.onNewComment = [function(comment) {
$.post("sendnotification", { comment: comment.id, post: $post->id,author:$author->id}, function(result){
alert(result);
});
}];
};
I know I need to end something like here but I am stuck how to use post datas(comment,post,author) inside this function
$.ajax({
url: 'sendnotification',
type: 'POST',
data: 'query=' + query ,
dataType: 'JSON',
async: true,
success: function(data){
process(data)
}
Thanks
Just use the same object literal you did for $.post, eg (gotta assume that's some PHP or something in there)
$.ajax({
url: 'sendnotification',
type: 'POST',
data: { comment: comment.id, post: {$post->id}, author: {$author->id} },
dataType: 'json',
async: true,
success: function(data){
process(data)
}
});
I believe dataType: 'JSON' should be changed to dataType: 'json'
Also, use the same data array as you used in your $.post variant.
$.ajax({
url: 'sendnotification',
type: 'POST',
data: { comment: comment.id, post: $post->id,author:$author->id } ,
dataType: 'json',
async: true,
success: function(data){
process(data)
}
});

Send and receive data in same ajax request with jquery

What is the best way to send data and receive a response dependent on that data?
Consider the PHP file used for the request:
$test = $_POST['test'];
echo json_encode($test);
I have tried unsucessfully to achieve this with:
$.ajax({
type: "POST",
dataType: "json",
data: '{test : worked}',
url: 'ajax/getDude.php',
success: function(response) {
alert(response);
}
});
Lose the quotes to pass the object:
$.ajax({
type: "POST",
dataType: "json",
data: {test : worked},
url: 'ajax/getDude.php',
success: function(data) {
alert(data);
}
});
Instead of this
data: '{test : worked}'
try
data: {"test" : worked} // Worked being your data you want to pass..
data: {"test" : "worked"} // Else enclose worked in quotes
The problem appears to be that you're submitting a string rather than a json object - change data: '{test : worked}' to data: {test : 'worked'}

problem with ajax call of json and php

i have one AJAX function getting results from php file as bellow
$.ajax({
type: "POST",
url: "GeteT.php",
cache:false,
data:"id="+ encodeURIComponent(1),
dataType:'json',
success: function(json)
{
g_foo = json.foo;
}
});
now i want to use the value of g_foo but i can't i try using console.log but i am facing strange problem. like
if i use function like that
$.ajax({
type: "POST",
url: "GeteT.php",
cache:false,
data:"id="+ encodeURIComponent(1),
dataType:'json',
success: function(json)
{
g_foo = json.foo;
console.log(g_foo);
}
});
now i can see the value return from php file
if now i use function like that
$.ajax({
type: "POST",
url: "GeteT.php",
cache:false,
data:"id="+ encodeURIComponent(1),
dataType:'json',
success: function(json)
{
g_foo = json.foo;
}
});
console.log(g_foo);
now i got error undefined g_foo;
thanks
As ajax is asynchronous, there's no way of making sure that g_foo is available as soon as you call the $.ajax() request, hence it's not available outside that callback.
You could however, specify async:false to that .ajax() call, but it will most likely block the browser, attempting to wait for the request, something I don't think you want to happen.
To use it outside you have two ways:
1)make the call syncronous like this (this is not a best practice as the browser has to wait for the call to continue the flow):
$.ajax({
type: "POST",
url: "GeteT.php",
cache:false,
data:"id="+ encodeURIComponent(1),
dataType:'json',
async: false,
success: function(json)
{
g_foo = json.foo;
}
});
console.log(g_foo);
2) call a function on success and continue the flow from there using whatever data has been returned from the function (this is the best practice)
$.ajax({
type: "POST",
url: "GeteT.php",
cache:false,
data:"id="+ encodeURIComponent(1),
dataType:'json'
success: function(json)
{
g_foo = json.foo;
yourfunction(g_foo);
}
});
function yourfunction(g_foo){
//here g_foo is set
}

Categories