这是来自以下方面的扩展问题:从多个复选框获取$ _POST
我正在尝试获取每个选中复选框的标签,然后将其显示为:
“销售|工程”(不带引号)
或者,如果仅选中一个复选框:
“销售量”
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(result.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
您应该使用数组类型命名loc_chks [],这也允许您为每个项目使用特定的键,例如:loc_chks[sales]
对于PHP,您可以在$_POST["loc_chks"]["sales"]
HTML(myform.html)中使用它:
<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/>
在PHP中,您可以使用implode
funciton,它使您可以将数组项与粘合字符串(作为第一个参数)
PHP(result.php)连接起来:
<?php
session_start();
if(!empty($_POST["loc_chks"]) and is_array($_POST["loc_chks"])) {
$loc = implode(' | ',$_POST["loc_chks"]);
}
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句