Sending object via $.post - php

I have an object saved in the variable called items.
0: Object
name: "Jim"
age: "42"
1: Object
name: "Bill"
age: "50"
When trying to post
$.post("mypage.php", items, function(data)
I get an empty post variable in the php page, but this returns post variables
$.post("mypage.php", "name=jim", function(data)
What am I doing wrong?
Edit it is an array of objects so if I pass
$.post( "mypage", items[0], function( data) {
I get a results but
$.post( "mypage", items, function( data) {
print_r is empty

It looks like the object you're posting may be an array with this format:
items = [
{
name: "Jim",
age: "42"
},
{
name: "Bill",
age: "50"
}
]
The docs seem to indicate that you must pass a plain object. You probably want to post something with the format:
items = {
people: [
{
name: "Jim",
age: "42"
},
{
name: "Bill",
age: "50"
}
]
};
Then you should be able to access the data via $_POST['people'].
UPDATE
To be accurate, you can post your array just fine, it will generate this post Jim=&Bill=
Almost surely not what you want. However if you follow the syntax specified by jQuery.param, your array will be treated correctly. Should be and array of objects with name and value:
items = [
{
name: "Jim",
value: "42"
},
{
name: "Bill",
value: "50"
}
]

You are using a curious behaviour of jQuery. It's found in the $.param function, which is used behind the scenes to prepare an AJAX query from your data.
If you send an array, jQuery expects it to be an array of objects, each of which has two keys, name and value. You are obviously not providing data in that structure. Your script actually sends data that looks like this:
Jim=undefined&Bill=undefined
If you do print_r($_POST['Jim']); in PHP, you will get the string undefined.
You obviously don't want this. You need to send the array within an object.
{
people: [
{
name: 'Jim',
age: 42
},
{
name: 'Bill',
age: '50'
}
]
}
This way your data will be serialised as this:
people[0][name]=Jim&people[0][value]=42&people[1][name]=Bill&people[1][age]=50
If you do print_r($_POST['people']);, you will get a meaningful result.

Related

JSON getting data from multidimensional arrays

I have been populating a chart embed with data from a multidimensional PHP array ($result). When printing the array to JSON (using print json_encode($result, JSON_NUMERIC_CHECK);) i got the following array structure:
[
{
"name":"Array1",
"data":[
1,
2,
3
]
},
{
"name":"Array2",
"data":[
1,
2,
3
]
}
]
I used this array to populate my highcharts in the code below. This used to work just fine but after I changed the setup of my array it will now have to be reworked.
$.getJSON("../data.php", {id: escape(tableName)}, function(json) {
chartOptions.chart1.xAxis.categories = json[0]['data'];
chartOptions.chart1.series[0].data = json[1]['data'];
});
The new setup of my $result array after making some changes is the below:
{
"Array1":{
"data":[
"1",
"2",
"3"
]
},
"Array2":{
"data":[
"1",
"2",
"3"
]
}
}
As such, the code I used to populate my Highcharts no longer works. I would very much appreciate if anyone can help me understand how I can rework the $.getJSON code such that it would work with the new array structure. Or maybe inform me if I have to stick with the old array setup? Thanks.
From what I can tell (haven't tested), you just need to change:
$.getJSON("../data.php", {id: escape(tableName)}, function(json) {
chartOptions.chart1.xAxis.categories = json[0]['data'];
chartOptions.chart1.series[0].data = json[1]['data'];
});
To
$.getJSON("../data.php", {id: escape(tableName)}, function(json) {
chartOptions.chart1.xAxis.categories = json['Array1']['data'];
chartOptions.chart1.series[0].data = json['Array2']['data'];
});
The change in the JSON structure changed from an array of dictionaries, to a dictionary of dictionaries, so you no longer access it via index, instead you access it by key (Array1, Array2).

MongoDB update not working with existing array [duplicate]

In MongoDB how do you use $set to update a nested value?
For example, consider a collection people with the following document:
{
_id: ObjectId("5a7e395e20a31e44e0e7e284"),
name: "foo",
address: { street: "123", town: "bar" }
}
How do I update the street field embedded in the address document from "123" to "Main Street"?
Using the dot notation:
db.people.update({ }, { $set: { "address.street": "Main Street" } })
In addition to Niels' answer, also do verify the "type" of the nested value. In my case, it was a "string" formed from json. Though this might be unlikely, but do ensure that the value has the right type.

Parse JSON without relying on order

I have this JSON, I'm parsing. I can retrieve all the values of field_data in order in PHP, but I need to traverse and get the values I wan't by specifying the field name, without concern for order:
JSON
{
created_time: "2018-05-14T16:11:02+0000",
id: "555555555555",
field_data: [
{
name: "bedrooms_interested_in?",
values: [
"2"
]
},
{
name: "When_are_you_looking?",
values: [
"January 1s 2019"
]
},
{
name: "email",
values: [
"xxxx#domain.com"
]
},
{
name: "full_name",
values: [
"John Michael"
]
},
{
name: "phone_number",
values: [
"+15555555555"
]
}
]
}
PHP
This is grabbing the last 3 fields. I'm currently able to pull the fields_data in order like this, but how could I grab the value fields I want by specifying the name field I'm looking for?
$data = json_decode($response, true);
// var_dump($data);
file_put_contents('dump.txt', $response);
$lead_email = $data['field_data'][2]['values'][0];
$lead_fullname = $data['field_data'][3]['values'][0];
$lead_phone = $data['field_data'][4]['values'][0];
You can use array_column to re-index an array by a given column, which will give you easier and more reliable access to the data:
$data['field_data'] = array_column($data['field_data'], null, 'name');
This allows you to access fields by name instead of their numeric index, e.g.
echo $data['field_data']['email']['values'][0];
// xxxx#domain.com
See https://eval.in/1003874 for a full example
(Note, I added some quotes to your JSON property names, since they were missing in the question. I assume that's a formatting issue, since this wouldn't work at all otherwise)

Encode Binary Tree to Json

I've stored a bunch of data in my database to draw a binary tree in a html canvas
Idx / Name
1 Apple
2 Bee
3 Cafe
4 Diamond
8 East
9 Game
16 Hobby
Here, idx represents the location of items in a binary tree. So the data above looks something like this in a tree
1.Apple
/ \
2.Bee 3.Cafe
/
4.Diamond
/ \
8.East 9.Game
/
16.Hobby
Now, I need to encode that database rows into a json format:
{
id: "1",
name: "Apple",
data: {},
children: [{
id: "2",
name: "Bee",
data: {},
children: [{
id: "4",
name: "Diamond",
data: {},
children: [{
// East/Game/Hobby comes here in the same manner...
}]
}]
},
{
id: "3",
name: "Cafe",
data: {},
children: [] // has no children
}]
}
What I've tried was creating an array of arrays and going through all the values descending order by grab a vale and put it into its parent array and remove it from the array. So, my pseudo code was something like this...
nodeArray = [1,2,3,4,8,9,16]; <-each node is an object with needed data contained.
treeArray = [........] <- arrays with key=>each index / value=>empty
while(nodeArray size is larger than 1) // 1 = the top most value
{
grab the last node from nodeArray
parent_idx = (int)(last one id / 2)
push the last node into the treeArray[parent_idx]
pop the used index
}
Then, I will have treeArray something like this
treeArray = [
1:[2,3]
2:[4]
4:[8,9]
8:[16]
]
...this is not the array-converted binary tree I was looking for.
So, I need to go through treeArray in desc order and relocate them... Yup. I know I'm getting messed up here :( It's getting more complicated and harder to understand.
Would be there more elegant and simpler way to do it? :(
I ended up using javascript and loop through each node and calling the following function
var objlist = {};
function buildTree(id, parent_id, data)
{
if(id in objlist) alert("It already exists!");
objlist[id] = { id: id, data: data, children: [] };
if (parent_id in objlist)
{
objlist[parent_id].children.push(objlist[id]);
}
}
where parent_id is id/2.

Textarea list via jquery .ajax to php

I have a textarea that I want to submit via ajax. When I try to output the value, I only get [object Object]
Jquery (ajax)
$("#insertAddresses").click(function() {
$.ajax({
type: "POST",
url: "insertAddr.php",
data: 'addresses=' +
}).done(function(list) {
//getList(); // run query to get addresses and populate list
});
});
PHP (i've tried)
$_POST['addresses'];
or
$addresses = explode("\n", $_POST['addresses']);
Regardless of anything i've tried, always returns
[object Object]
Help?!
Your serverscript is returning a json object, which is correctly recognized by JavaScript as an object. You can do a whole lot of things with that object, but you can't just put it on your website, as it is not html or text.
Here is a short description of json: http://en.wikipedia.org/wiki/JSON
I don't know how your data is structured, so i can't tell you how you can access your data. But in a json like this (example from wikipedia):
{
"firstName": "John",
"lastName" : "Smith",
"age" : 25,
"address" :
{
"streetAddress": "21 2nd Street",
"city" : "New York",
"state" : "NY",
"postalCode" : "10021"
},
"phoneNumber":
[
{
"type" : "home",
"number": "212 555-1234"
},
{
"type" : "fax",
"number": "646 555-4567"
}
]
}
You could, ie., excess the firstName simply with:
data.firstName
An voila, there is your excpected data.
You're data should be an object:
data: { adresses: "value" }
Just a little tip: the shorthand ajax-call for what you're doing in jQuery is $.post(.... and then you can lose the "type". Does exactly the same, but I think it's just a little neater.

Categories