我在jQuery中有一个onclick和post函数,我将两个变量传递给我的PHP函数。从javascript发送数组到PHP函数并读取它的值
功能看起来像以下:
$(".pop-save-us").click(function(){
console.log(checkbox_events);
console.log(user_id);
// alert("Button save pressed");
$.post('/user/AddRemoveBrandUsers', { brands: JSON.stringify(checkbox_events),userId:user_id} , function(data) {
if(checkbox_events.length==0)
{
alert("No changes were made.")
}
else {
if (data == "ok") {
alert("ok");
}
}
});
});
第一个值是在以下格式的数组:
的console.log(checkbox_events)给出以下输出:
[2: "checked", 4: "checked", 5: "checked"]
我请执行JSON.stringify(checkbox_events)将我的数组转换为JSON格式并将其传递给我的PHP函数,如下所示:
public function AddRemoveBrandUsersAction()
{
$brands = $this->getRequest()->getPost("brands");
$userId = $this->getRequest()->getPost("userId");
for($i=0;$i<count($brands);$i++)
{
// how can I now access each value of the brands array
// I need both key and value... How do I access them ??
}
die("ok");
}
所以,你想使用一个'foreach($ array为$ key => $ value)'? – Epodax
好的,你能回答一个答案的形式,以便我可以接受你的回答....我怎样才能得到循环内的“检查”值? – perkes456
“检查”值是什么意思?如果您指的是复选框或收音机,那么值/输入仅在被检查时才被发送? – Epodax