I have the following JSON string which I want to store its content in a table in my MySQL database using PHP:
[{
"personal": "1001",
"content": [{
"language": "English",
"words": "302",
"approved": "yes"
}, {
"language": "Spanish",
"words": "180",
"approved": "no"
}]
}, {
"personal": "1002",
"content": [{
"language": "Spanish",
"words": "236",
"approved": "yes"
}]
}]
To do this I try to send this JSON string to PHP using AJAX, the variable sjson
is the one that stores the JSON string:
$.ajax({
type: "POST",
dataType: "json",
url: "receive.php",
data: {myData:sjson},
success: function(data){
console.log(data);
},
error: function(e){
console.log(e.message);
}
});
In my PHP page I capture the variable:
<?php
$obj = $_POST['myData'];
?>
This is when I'm a bit lost, since the problem is that I can't access the values of the items personal
and content
, I've tried with a foreach
but I get a resultundefined
I want to store this information in my MySQL table personal
as follows:
id_personal | language | words | approved
--------------------------------------------
1001 | English | 302 | yes
1001 | Spanish | 180 | no
1002 | Spanish | 236 | yes
How can I achieve it?
I will appreciate your help.
You can use the json_decode function to use the variable values you receive in php: