First of all i am new to php and javascript
I have a web form where users can add multiple contacts ans send to ther server.
Due to some reasons i cant use the normal html elements to store the values ,so i am using an array to store values.
//init array
var contacts = new Array(); //contact array
var tempArray = new Array(); //temp array to store current contacts
//getting the contact info and setting to a temp array
tempArray = {
name:"username",
age:12,
sex:false
};
//push the content to the `contacts` array
contacts.push(tempArray);
I added many contacts to the contacts
array and now i need to submit the array to server.
Problem
I am using Codeignitor and Malsup FORM plugin.
as per malsup i can configure the data option like this
var options = {
dataType:'json', //type of data
data:[contactArray:contacts], //additional parm
};
and on ajaxSubmit
option i can give this option as a parm.
when i do this i am getting the following error
uncaught exception: [Exception... "Component returned failure code: 0x80460001 (NS_ERROR_CANNOT_CONVERT_DATA) [nsIDOMFormData.append]" nsresult: "0x80460001 (NS_ERROR_CANNOT_CONVERT_DATA)" location: "JS frame :: /js/form.js :: fileUploadXhr :: line 224" data: no]
temp/jquery.min.js
Line 4
IT WORKS with $.POST
in jQuery.
so i tried the JSON.stingify()
to convert the data to string.
but on server i am getting like this
'contactArray' => string '[{"name":"username","sex":"12","sex":"false"}]'
If i used the json_decode
then i cant use the form validation.
I want to use the FORM VALIDATION LIBRARY IN CODEIGNITOR.
CI supports validation of array of elements.
so
if i get something like name[],age[],sex[]
then i can validate easily.
Please help me to solve the problems or give me suggestions.
Thank you.
data:[contactArray:contacts]
is not valid JavaScript. You probably meantdata: {contactArray:contacts}
– Utkanos Jul 9 '12 at 11:07