简体   繁体   中英

Send array with checked Checkbox value and retrieve as string in php

I am sending checked checkboxes value to php in array.

// tag =['Apple','Mango','Tomato']
var tag =  $(this).children().siblings().children().children('input[name="cb"]:checked');
        var tagData = [];
        $.each(tag, function() {
            tagData.push($(this).val());
        });

        console.log(tagData);
$.ajax({
    type: "POST",
    url: "script.php",
    data: {tag: tagData }, 
    cache: false,

    success: function(){
        alert("OK");
    }
});

Console.log data

(2) ["Apple", "Apple"]
0: "Apple"
1: "Apple"
length: 2__proto__: Array(0)

I'm getting this array in php like this.

 $list = $_POST['tag'];
 $imgTag = implode( ", ",$list);
 // i want like this - $imgTag = "Apple,Mango,Tomato".

But i getting empty line in php.

Serialize your tagData using JSON.stringify while sending in ajax request. like this

$.ajax({
    type: "POST",
    url: "script.php",
    data: {tag: JSON.stringify(tagData) }, 
    cache: false,

    success: function(){
        alert("OK");
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM