user1277467
user1277467

Reputation: 199

Seriazlizing arrays in Ajax for php

i try to serialize my checkbox items.

first, i want to tell you what i want.

i have a form like that,

enter image description here

User select checboxes and when press the button "Sil" it has to remove the artist.

i prepare an ajax script for that,

function deleteData2()
{
    var artistIds = new Array();

    $(".p16 input:checked").serialize()(function(){
        artistIds.push($(this).attr('id'));
    });


    $.post('/json/crewonly/deleteDataAjax2', { 'artistIds': artistIds },function(response){
        if(response=='ok')
            alert("ok");
    });


}

My ajax script has movie id and artist id, it has these datas. The actual problem is that, ajax cannot send this data to php. I make some research about it and then i reached that i have to serialize my array and add my $(".p16 input:checked").serialize()(function(){ script serialize but also does not works.

public function deleteDataAjax2() {

        extract($_POST);

        if (isset($artistIds))
            $this->sendJSONResponse('ok');

    }

above code is my php. and artistIds is always empty so my serialize function does not work.

What might be wrong ? How can i reach my artistIds in php side?

EDIT

finally, i come to this

  var artistIds = new Array();

    $(".p16 input:checked").each()(function(){
        artistIds.push($(this).attr('id'));
    });


    $.post('/json/crewonly/deleteDataAjax2', JSON.stringify({ 'artistIds': artistIds }),function(response){
    if(response=='ok')
        alert("ok");
    });

and in php side,

public function deleteDataAjax2() {

         extract($_POST);

         $a = json_decode($artistIds);

        if (!empty($a))
            $this->sendJSONResponse('ok');

    }

it's still wrong

Upvotes: 1

Views: 98

Answers (2)

Saket Patel
Saket Patel

Reputation: 6683

$(".p16 input:checked").serialize()(function(){
    artistIds.push($(this).attr('id'));
});

this is still giving you an array of artistids, so you can't send it to server, you need to convert it into JSON format first then you can submit it to server like this

$.post('/json/crewonly/deleteDataAjax2', JSON.stringify({ 'artistIds': artistIds }),function(response){
    if(response=='ok')
        alert("ok");
});

and make sure to decode that json data on server before using it using json_decode function

Upvotes: 2

marspzb
marspzb

Reputation: 364

$(".p16 input:checked").serialize()(function(){
artistIds.push($(this).attr('id'));
});

I think these lines don't work, since I've tried them on Chrome and they give me an error. I think you may want to use the each function of jQuery for doing something like:

$(".p16 input:checked").each(function(){
   artistIds.push($(this).attr('id'));
});
 $.post('/json/crewonly/deleteDataAjax2', { 'artistIds': artistIds },...

After that you'll have the $artistIds set as a PHP array.

The extract method isn't recomended for using with request variables since it can introduce some security flaws.

Upvotes: 1

Related Questions