i have a form to which the form elements are appended using the onclick event.
the form elements along with their values and label are then maintained in an array .
function save_form() {
var x = document.getElementById("formBuilder");
var elements_data = new Array;
for (var i = 0; i < x.length; i++) {
if (x.elements[i].type == 'checkbox' || x.elements[i].type == 'radio') {
var all_ele_value = [];
var ele_id = x.elements[i].id;
$("input:" + x.elements[i].type + "[name=" + ele_id + "[]]").each(function () {
all_ele_value.push($(this).val());
});
var end_value = all_ele_value.join(";");
}
else if (x.elements[i].type == 'select-one') {
var ele_id = x.elements[i].id;
var end_value = $("#" + ele_id + " option").map(function (i, n) {
return n.value;
}).get().join(";");
}
else {
var end_value = x.elements[i].value;
}
}
var form_data = JSON.stringify(elements_data);
$.post(software_url + 'form_manager/save_form', {
'form_data': form_data
}, function (data) {
alert(data);
});
}
now i want to encode form_data in my php file . I want to know a way to do that because json_decode does not work. Iam getting all the output in json format .BUt i want to iterate through it.
I’m strongly going to recommend two things to you.
One is learning about jQuery.serialize, which does the heavy lifting work you did to put all the elements together in a string.
Moving right along, json_decode does work, unless the JSON string is invalid, check what you are receiving and test it your JSON syntax on JSONLint – to see if it’s correct.
In php, you have to use json_decode, and then you can go through it.