2016-01-10 24 views
0

我順利地通過與AJAX POST方法如下數值來我的PHP文件PHP不能發送貼出的數組元素

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

的問題是,下面的代碼發送的HelloArray代替HelloBooksDocuments。你能幫我找到我的錯誤嗎?

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

http://stackoverflow.com/questions/34298832/count-of-array-showing-it-is-full-but-not-elements-are-inside/34298869#34298869 –

回答

2

替換此行:

$comments= $comments.$categories; 

有了:

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

的原因是,該變量$類別是一個數組,你需要將其轉換爲字符串。

這個你可以用implode做。如果你想讓它們用逗號分隔,那麼把它作爲第一個參數傳遞,替換上面我建議的空字符串「」

當然,你可以改變它,並使用你選擇的另一個分隔符。

+0

謝謝,我會在8分鐘內接受 – EducateYourself

+1

使用他的預期輸出,也許將分隔符更改爲'「」'? –

+0

的確,@ iam-decoder,感謝您指出了這一點。 – trincot