a few months ago I posted this thread on how to grab an array with ajax and PHP…
got the solution:
to send via AJAX:
data: 'colors='+JSON.stringify(arrColors)
to get in server:
$colors = json_decode($_POST['colors'],true);
this worked, however… not quite… as this sends the array as a string not as an array… I had noticed when tried to print it in PHP, some weird behaviors (regular loop just prints ‘00000’ & things like that…) I couldn’t figure out, then realized it’s sent as a string… also, typeof data in AJAX code prints ‘string’…
also: when print with a foreach loop it prints all items in array fine, but get two warnings;
Undefined index: colors in /Library/WebServer/Documents/....../array.php on line 35
Warning: Invalid argument supplied for foreach() in /Library/WebServer/Documents/....../array.php on line 39
so I knew there had to be something wrong…
also: doesn’t JSON.stringify turn the array into a string? this is precisely what I don’t want… I want it sent as an array…
so: how do you grab an array of strings in PHP?? how to get, for example, values from a set of checkboxes in PHP, or something of this sort?
what is the PHP equiv of this method in Java?
request.getParameterValues("colors[]");
this is specif to get an array from the request (diff method from what you use to get a single string…) what is the PHP equiv to this java method to get an array from the request?
foreach($arrColors as $element) {
echo $element . '<br>';
}
[/code]
but get two warnings:
Notice: Undefined index: colors in /Library/WebServer/Documents/amigo_secreto/array.php on line 34
Warning: Invalid argument supplied for foreach() in /Library/WebServer/Documents/amigo_secreto/array.php on line 44
however, the `foreach` loop prints elems of array fine.. why do I get these warnings?
and this loop
var arrColors = ["red","blue","green","yellow"];
$.ajax({
url:"array.php",
type:"post",
data: {colors:arrColors},
success:function(data){
console.log('datatype: ' + typeof data); // prints "string"
$("#outputArray").append( data );
},
error: function() {
$("#outputArray").append('fail');
}
});
that’s it…
am I missing something?
(I asked about how you get values from a set of check boxes, just to see how you get an array with PHP sent from the request, in general…)
This may be easier. Let me work on it.
LOL two months ago I knew very little (nothing) about jQuery, now I feel I can do anything.
If you want values from a form, let me know.
Answer may be tomorrow, but I will have an answer based on what I have done based on form input.
Hard coded colors don’t make sense.
Show me your form.
it’s not a form at all… it will be an array (not hard-coded, but anyway, an array, with approx 8 items (actually, one item will be chosen (the first item) when a button is pressed, then the array minus the first item will be sent, then process repeated again (array will be shuffled when button is pressed, then first item in array chosen, then when another button is pressed, array will be shuffled again, and another name (first item in array) will be chosen, etc… and every time after first item is chosen the array is sent to the server, then sent back, minus the last item chosen (always first elem in array…) you get my drift? bottom line: it’s an array, with about 8 items in it…)