在Ajax中序列化php数组

时间:2022-11-16 21:31:00

i try to serialize my checkbox items.

我尝试序列化我的复选框项。

first, i want to tell you what i want.

首先,我想告诉你我想要什么。

i have a form like that,

我有一个这样的表格,

在Ajax中序列化php数组

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

用户选择复选框,当按下“Sil”按钮时,它必须删除艺术家。

i prepare an ajax script for that,

我为此准备了一个ajax脚本,

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.

我的ajax脚本有电影id和艺术家id,它有这些数据。实际的问题是,ajax不能将这些数据发送到php。我对它做了一些研究,然后我发现我必须序列化我的数组并添加$(")p16输入:勾选").serialize()(函数()

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.

上面的代码是我的php。而artistIds始终为空,所以我的serialize函数不起作用。

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

有什么问题吗?如何在php中访问我的文章?

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,

在php的一面,

public function deleteDataAjax2() {

         extract($_POST);

         $a = json_decode($artistIds);

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

    }

it's still wrong

它仍然是错的

2 个解决方案

#1


2  

$(".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

它仍然给你一个artistids数组,所以你不能将它发送到服务器,你需要先将它转换成JSON格式,然后你可以像这样提交给服务器

$.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

并确保在使用json_decode函数之前对服务器上的json数据进行解码

#2


1  

$(".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:

我觉得这几行不行,因为我在Chrome上试过了,他们给了我一个错误。我认为您可能想要使用jQuery的每个函数来做以下事情:

$(".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.

之后,您就可以将$artistIds设置为一个PHP数组。

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

提取方法不能用于与请求变量一起使用,因为它会引入一些安全缺陷。

#1


2  

$(".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

它仍然给你一个artistids数组,所以你不能将它发送到服务器,你需要先将它转换成JSON格式,然后你可以像这样提交给服务器

$.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

并确保在使用json_decode函数之前对服务器上的json数据进行解码

#2


1  

$(".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:

我觉得这几行不行,因为我在Chrome上试过了,他们给了我一个错误。我认为您可能想要使用jQuery的每个函数来做以下事情:

$(".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.

之后,您就可以将$artistIds设置为一个PHP数组。

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

提取方法不能用于与请求变量一起使用,因为它会引入一些安全缺陷。