serialise array will inputs of same name
i've got 3 inputs, 2 of which i want in the same array (name="notes['a']" and name="notes['b']") but when i use jquerys' serializeArray() it puts them all at the same 'level'. But i want to use php to serialise the posted notes array into the database using the method thats been working for inputs that aren't part of an array (ie name="basic"
foreach($_POST['data'] as $var => $value) {
if(!strstr(strtolower($value['name']),'added')) $q1 .= "".$value['name']."='".$value['value']."',";
}
echo $q = "UPDATE ".$_POST['table']." SET ".rtrim($q1,',')." WHERE ".$_POST['idField']."=".$_POST['id']." ";
*the array $_POST['data'] is getting posted as;*
[data] => Array
(
[0] => Array
(
开发者_开发技巧 [name] => notes[\'a\']
[value] => dan@jynk.net
)
[1] => Array
(
[name] => notes[\'b\']
[value] => Daniel Crabbe
)
[2] => Array
(
[name] => test
[value] => Daniel Crabbe
)
)
but i guess it should be along these lines?
[data] => Array
(
[0] => Array{
Array(
[name] => notes[\'a\']
[value] => dan@jynk.net
)
Array
(
[name] => notes[\'b\']
[value] => Daniel Crabbe
)
)
[1] => Array
(
[name] => test
[value] => Daniel Crabbe
)
)
how can i get jquery to respect the arrays in the input array? any help welcome...
UPdates no got it like this but want everything on level [0] for easy access...
Array
(
[0] => Array
(
[name] => cm_email_to
[value] => dan@jynk.net
)
[1] => Array
(
[name] => cm_name_to
[value] => Daniel Crabbe
)
[2] => Array
(
[name] => cm_moveToList
[value] =>
)
)
*inputs*
<input id="cm_email_to" name="cm_email_to" value="dan@jynk.net" class="toPostCM" />
<input id="cm_name_to" name="cm_name_to" value="Daniel Crabbe" class="toPostCM" />
*jquery*
var dataCMSettings = $(".toPostCM").serializeArray();
Try taking the array indicies out of the input name:
<input name="notes[]" value="val1" />
<input name="notes[]" value="val2" />
Remove the quotes from your array indices in your HTML, so array['a']
becomes array[a]
.
ok - this does exactly what i need...
var params = {};
$('.toPostCM').each(function(index,value) {
params[value.name] = value.value;
});
console.log(params);
*gives me*
Array
(
[cm_email_to] => dan@jynk.net
[cm_name_to] => Daniel Crabbe
[cm_moveToList] =>
)
thanks all...
精彩评论