a few weeks ago had the same problem with java… got a lot of responses, most of which involved json; but in the end the solution was very simple, & did not require json:
java:
String[] resp = request.getParameterValues("colors[]");
if (resp.length > 0) {
for (int i=0; i < resp.length; ++i) {
out.println(resp[i] + "<br>");
}
}
jquery:
var arrColors = ["red","blue","green","yellow"];
$.ajax({
url:"array.jsp",
type:"POST",
data: {colors:arrColors},
success:function(data){
$("#output").append( data );
},
error: function() {
$("#output").append('fail');
}
});
based on code here,
I changed ‘data’ line to:
data: "colors="+arrColors,
& was able to grab the array, but I can’t parse it:
php:
$arrColors = $_POST['colors'];
this
echo $arrColors;
prints
“red,blue,green,yellow”
and this
echo count($arrColors);
prints just
“1”
and I can’t parse it and print it like I do in java (with loop, inserting ‘<br>’ after each element…)
I also explode it (again, based on code in url above),
$arrColorsExpl = explode(', ', $arrColors);
and this
print_r($arrColors);
prints:
“Array ( [0] => red,blue,green,yellow )”
(& I can only print it with print_r, and not echo (???) )
(& again, I can’t parse it to print it however I want…)
& this
for ($i=0; $i < count($arrColors); $i++) {
echo $arrColors[i] + '<br>';
}
prints simply: “0”
& this
foreach ($arrColors as $element) {
echo $element . '<br>';
}
prints nothing…
I would like this to work with the same flexibility as in java… I would appreciate some help… (I have limited access to the internet now, probably won’t see any response(s) till Monday…)
thank you…