EducateYourself
EducateYourself

Reputation: 979

php cannot send posted array elements

I successfully passed following values with AJAX post method to my PHP file

name:John
email:[email protected]
comments:Hello
category_list[]:Books
category_list[]:Documents

The problem is that the following code sends HelloArray instead of HelloBooksDocuments. Could you please help me to find my mistake.

$email = $_POST["email"];
$name = $_POST["name"]);    
$comments = $_POST["comments"];
$categories = $_POST["category_list"];  //the problem is here  

Upvotes: 0

Views: 27

Answers (1)

trincot
trincot

Reputation: 350272

Replace this line:

$comments= $comments.$categories;

With:

$comments= $comments.implode("", $categories);

The reason is that that variable $categories is an array, and you need to convert it to a string.

This you can do with implode. If you want them separated by a comma, then pass that as the first argument, replacing the empty string "" I have suggested above.

Of course, you can change this, and use another separator of your choice.

Upvotes: 2

Related Questions