0

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.

8
  • 1
    What is the value in tagData variable in JS? Commented Jul 5, 2018 at 11:23
  • like this - ['Apple','Mango','Tomato'] Commented Jul 5, 2018 at 11:24
  • And what is the value of $list in php? Commented Jul 5, 2018 at 11:26
  • Print $_POST to check whether any data posted or not? Commented Jul 5, 2018 at 11:26
  • @LovepreetSingh $list echo "[object Object]" and i'm sending many variable and all getting posted. Commented Jul 5, 2018 at 11:31

1 Answer 1

1

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");
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.