I have a javascript array of data that I want to submit to my codeigniter database:
[
{
"name": "title",
"value": "myTitle"
},
{
"name": "content",
"value": "myContent."
}
]
I want to submit this data to codeigniter, and have it update the database. Note that this is not data coming from a form, so I can't just use the typical code igniter form_open() / serialize() method. I am using an ajax post and dynamically building the data:
var submissionData = [];
instanceFields.each(function(index){
var $thisField = $(this);
var thisData = {};
thisData.name =$thisField.attr('data-name');
thisData.value = $thisField.text();
submissionData.push(thisData);
});
var submissionString = {arr: JSON.stringify(submissionData)};
var submissionURL = baseURL + 'instances/edit';
$.ajax({
type: "POST",
url: submissionURL,
data: submissionString,
success: function(data){
console.log("success:",data);},
failure: function(errMsg) {
console.error("error:",errMsg);
}
Normally, for data posted from a form, I could access it like this in code igniter:
$this->input->post('title')
But if I echo that out here, I get an empty string.
I would then insert the data like this:
$data = array(
'title' => $this->input->post('title'),
'content' => $this->input->post('content'),
);
return $this->db->insert('extra_instances', $data);
If I decode the JSON and do a var dump, it looks like this:
success: array(5) {
[0]=>
object(stdClass)#19 (2) {
["name"]=>
string(5) "title"
["value"]=>
string(21) "myTitle"
}
[1]=>
object(stdClass)#20 (2) {
["name"]=>
string(7) "content"
["value"]=>
string(238) "myContent"
}
}
How can I modify this to insert into the database?