【发布时间】:2020-01-29 19:22:26
【问题描述】:
我的表单中有以下输入字段:
<label class="addFileSectionSelectArea">
<span class="addFileSectionSelectAreaText">לבנון</span>
<input type="checkbox" name="add_file[areas]" value="lebanon">
<div>
<svg viewBox="0 0 44 44">
<path d="M14,24 L21,31 L39.7428882,11.5937758 C35.2809627,6.53125861 30.0333333,4 24,4 C12.95,4 4,12.95 4,24 C4,35.05 12.95,44 24,44 C35.05,44 44,35.05 44,24 C44,19.3 42.5809627,15.1645919 39.7428882,11.5937758" transform="translate(-2.000000, -2.000000)"></path>
</svg>
</div>
</label>
<label class="addFileSectionSelectArea">
<span class="addFileSectionSelectAreaText">סוריה</span>
<input type="checkbox" name="add_file[areas]" value="syria">
<div>
<svg viewBox="0 0 44 44">
<path d="M14,24 L21,31 L39.7428882,11.5937758 C35.2809627,6.53125861 30.0333333,4 24,4 C12.95,4 4,12.95 4,24 C4,35.05 12.95,44 24,44 C35.05,44 44,35.05 44,24 C44,19.3 42.5809627,15.1645919 39.7428882,11.5937758" transform="translate(-2.000000, -2.000000)"></path>
</svg>
</div>
</label>
在PHP,我尝试检索(提交后)选中的复选框:
$debug = $_POST['add_file']['areas'];
当我这样做时:var_dump($debug) 它总是返回代表 last 选中复选框的值的字符串值(但我希望它返回所有选中复选框的数组)。
因此,例如,如果我同时选中 [value='syria'] 和 [value='lebanon'] 复选框,那么我只会在 var_dump 中得到一个值为“syria”的字符串(因为 syria 输入在 html 中的 lebanon 输入之后)。
这是为什么呢?
【问题讨论】:
-
执行
name="add_file[areas][]"使其成为一个值数组。尽管除非有原因,否则我会选择name="add_file_areas[]"并在$_POST['add_file_areas']中访问它。