这是一个扩展问题:Get $_POST from multiple checkboxes
我正在尝试获取每个选中复选框的标签,然后将其显示为:
“销售 | 工程”(不带引号)
或者,如果只选择了一个复选框:
“销售量”
HTML (myform.html):
<!DOCTYPE html>
<html>
<head></head>
<body>
<form name='myform' method='post' action='result.php'>
<label for='sales'>Sales</label>
<input type='checkbox' name='loc_chks[]' id='sales' value='Sales' /><br/>
<label for='insideSales'>Inside Sales</label>
<input type='checkbox' name='loc_chks[]' id='insideSales' value='Inside Sales' /><br/>
<label for='engineering'>Engineering</label>
<input type='checkbox' name='loc_chks[]' id='engineering' value='Engineering' /><br/>
<label for='fieldService'>Field Service</label>
<input type='checkbox' name='loc_chks[]' id='fieldService' value='Field Service' /><br/>
<label for='production'>Production</label>
<input type='checkbox' name='loc_chks[]' id='production' value='Production' /><br/>
<label for='warehouse'>Warehouse</label>
<input type='checkbox' name='loc_chks[]' id='warehouse' value='Warehouse'/><br/>
<br/>
<input type='submit' id='subBtn' value='Submit'/>
</form>
</body>
</html>
PHP(结果.php):
<!DOCTYPE html>
<html>
<head></head>
<body>
<p><?php
if(!empty($_POST["loc_chks"]) and is_array($_POST["loc_chks"])) {
echo implode(' | ',$_POST["loc_chks"]);
} else { echo "we have a loser";}
?>
</p>
</body>
</html>
*已编辑 - php 文件中的“if”语句始终返回 false,即使选中了多个复选框