Getting array from request with PHP

hi,

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?

thank you…

Hi maya90,

If you pass your array to the $.ajax() function like this:

data: {colors:arrColors},

then in PHP, all you have to do is:

$colors = $_POST['colors'];

and $colors will be a PHP array containing the colors.

thank you for your response…

ok, so here’s my ajax code:

     $.ajax({
                url:"array.php",
                type:"post",
                data: {colors:arrColors},
                success:function(data){
                    console.log('datatype: ' + typeof data);    //    types 'string'
                    $("#outputArray").append( data );
                },
                error: function() {
                    $("#outputArray").append('fail'); 
                }
            });

PHP:

[code]
$arrColors = $_POST[‘colors’];

    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
     for ($i=0; $i < count($arrColors); $i++) {
           echo $arrColors[$i] + '<br>'; 
        }
prints:

“000000000”

??????

and typeof data in AJAX “success” fn prints “string”…

thank you…

PS: the coding thing (turning text into code) does not work as well in this thingie as it did in previous SP forum…

Where or how are you getting “arrColors”?
I would guess check boxes on a form?

nope… it’s just a hard-coded array…

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…:wink:

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…)

thank you…

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? :wink: bottom line: it’s an array, with about 8 items in it…)

thank you very much for your help…

ok, I got it…

key is using this:

data: 'colors='+JSON.stringify(arrColors),

instead of

data: {colors:arrColors},

to send array via AJAX (which I was doing already, but wasn’t doing the right thing on the back-end); and

using this

$arrColors = json_decode($_POST["colors"]);

instead of

$colors = $_POST['colors'];

to get array in PHP…

:slight_smile:

thank you for your help, fretburner & lorenw…

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.