Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aureliee123's avatar

loop over an array sent with ajax to controller

Hello , i'm using an ajax post request to send an array ( selectedids) to the controller

$.ajax({

        type:'POST',

        url:'{{action('OrderController@ajaxRequestPost')}}',

        data: { "type":strUser , "_token": "{{ csrf_token() }}","ids":JSON.stringify(selectedIds)},

        dataType: 'json',
                    
        success:function(data){
        
            alert(data);

        },
        error: function(xhr, textStatus, thrownError) {
                alert(' Error');
        }
        
    });

but i cannot loop over it in the controller i tried json_decode but it didn't work

the only thing that worked is :

echo $request->get("ids");

it shows this : 14,21,12 how can i iterate over it to get all the ids

thank you

0 likes
16 replies
Sinnbeck's avatar

Try this and post the output

dd($request->get('ids')) ;
Vardhan_KSPL's avatar

As you said if the output is "14,21,12", json_decode("14,21,12") won't work because it is not a valid Json.

If you want to decode a Json array string, the passed input should be "[14,21,12]". This should decode to an array and you can iterate over it.

In your case, try this in your data key,

data: { "type":strUser , "_token": "{{ csrf_token() }}","ids":"["+JSON.stringify(selectedIds)}+"]",

Also, you can refer json validator sites like https://jsonlint.com/ to check for any input is a valid json so that it can be decoded using json_decode method.

aureliee123's avatar

this didn't work i tried to echo echo $request->get("ids"); and i gives 12,13 and when i tried json_decode i got error 500 internal server error

Sinnbeck's avatar

Well if they are just comma separated you can just do

$array = explode(',' $request->get('ids'));
aureliee123's avatar

i tried it but it gives me error 500 internal server error

Sinnbeck's avatar

My best bet is you have a typo. Missing ' or ;

aureliee123's avatar

well i just tried this

data: { "type":strUser , "_token": "{{ csrf_token() }}",ids:JSON.stringify(selectedIds)}, removed the " " from ids

and the output is this [["12","14","15"]]

the json_decode still not working

jlrdw's avatar

Or just send a regular array in the post.

Sinnbeck's avatar

It is most likely already decoded. Just try looping over it (or use dd() to check the data. It is much better than echo)

Sinnbeck's avatar

So if you dd the output, what do you get exactly?

jlrdw's avatar

@sinnbeck will dd work with ajax, or will browser developer tools be a better choice.

jlrdw's avatar

Why don't you use browser developer tools to help troubleshoot.

aureliee123's avatar

so basically if i use JSON.stringify and then try to use json_decode i get the error array to string conversion same thing happens when i don't use JSON.stringify and try to access it as an array

aureliee123's avatar

i solved it using selected = JSON.parse("[" + selectedIds + "]")

and then

data: { "type":strUser , "_token": "{{ csrf_token() }}",ids:JSON.stringify(selected)},

and on php i used json_decode

thank you for your help

Please or to participate in this conversation.