这是我的代码:
var params = '_SaveEditUserReq=1' ;
for(i=0; i<document.editUserForm.elements.length; i++)
{
// if(document.editUserForm.elements[i].name.substr(0, 7) == "school|") {
if (document.editUserForm.elements[i].type == 'checkbox') {
// If the id begins with 'school|', take the value if it is checked.
if (document.editUserForm.elements[i].checked) {
console.log("CHECKED!" + document.editUserForm.elements[i].value);
params += "&" + encodeURIComponent(document.editUserForm.elements[i].name) + "=" + encodeURIComponent(document.editUserForm.elements[i].value) ;
console.log(params);
}
}
else {
// Take everything else
params += "&" + document.editUserForm.elements[i].name + "=" + document.editUserForm.elements[i].value ;
}
}
以下是正在构建的内容:
SaveEditUserReq=1&_username=jgald&_orig_username=jgald&password_error_msg=&new_password_ok=YES&new_password=&new_password_text=&new_password_too=&new_password_too_text=&_last_name=TheSirName&_first_name=TheName&_middle_initial=&_profile=Teacher&_email=email@email.com&_teacher=&school%7C=Baker%20Elementary%20School&school%7C=Brookland%20Middle%20School&school%7C=Charles%20M.%20Johnson%20Elementary%20School&=SAVE CHANGES&=CANCEL&_site=TheSite
当它传递给PHP
时,$_POST
数组只会看到最后checkbox
个。{/ p>
在这种情况下:
[school|] => Charles M. Johnson Elementary School
你能看出出了什么问题吗?
答案 0 :(得分:1)
给定多个具有相同名称的键/值对,PHP将丢弃除最后一个之外的所有键/值,除非键名以字符[]
结尾。
如果存在这些字符,那么它将使用名称不包含$_GET
的数组填充[]
(或发布等)。
即
foo.php?bar%5B%5D=1&bar%5B%5D=2
会给:
$_GET['foo'][0] == 1;
$_GET['foo'][1] == 2;
答案 1 :(得分:0)
从JavaScript部分,您有两个问题
编码名称
您不对名称进行编码,只编码值
params += "&" + encodeURIComponent(document.editUserForm.elements[i].name)
应该是
params += "&" + document.editUserForm.elements[i].name
未对其他内容中的值进行编码
您没有在else中编码值!
"=" + document.editUserForm.elements[i].value ;
需要
"=" + encodeURIComponent(document.editUserForm.elements[i].value) ;