我们说我有几个复选框选项:
<p class='question'>Reasons why it is down. Click as many as apply.</p>
<input type='checkbox' name='production-down' value='Decrease in demand' required>Decrease in demand <br>
<input type='checkbox' name='production-down' value='Expected Decrease in demand'>Expected Decrease in demand <br/>
<input type='checkbox' name='production-down' value='Technical difficulties in production'>Technical difficulties in production <br/>
<input type='checkbox' name='production-down' value='Shortage in raw materials'>Shortage in raw materials <br/>
<input type="checkbox" name="production-down" value='other' />Other
<input id='productionDownOther' name='other-production-down' type='text'/>
如果我要查看other
复选框。我应该为#productionDownOther
文本框提供与复选框相同的名称属性吗?这应该是怎么回事?
我计划使用php方法implode
将所选值一起添加到一列数据库中。
请告诉我我的计划是否错误,如果是/否告诉我原因。我在Web开发方面有点新鲜。仍然期待着学习。
答案 0 :(得分:1)
首先,您想要允许多种选择吗?或者一次只允许一个复选框。如果是第一个选项,则需要在每个复选框名称的末尾加上[]
,以便production-down
变为production-down[]
。这样,PHP会将所有值放入一个数组中,而不是用下一个值覆盖前一个值。如果您只想要一个选项,请将type="checkbox"
更改为type="radio"
。
至于其他选项,您可以执行以下操作:
<?php
$other = null;
if (in_array('other', $_POST['production-down']) || $_POST['production-down'] == 'other') {
$other = $_POST['other-production-down'];
}
// database stuff here
请注意,您应该在数据库输入之前始终清理数据。此外,检查帖子值是否确实存在是明智的,因此PHP不会发出通知。
答案 1 :(得分:0)
只需将name属性更改为:
<input type='checkbox' name='production-down[]' value='Decrease in demand' required>Decrease in demand <br>
<input type='checkbox' name='production-down[]' value='Expected Decrease in demand'>Expected Decrease in demand <br/>
<input type='checkbox' name='production-down[]' value='Technical difficulties in production'>Technical difficulties in production <br/>
<input type='checkbox' name='production-down[]' value='Shortage in raw materials'>Shortage in raw materials <br/>
<input type="checkbox" name="production-down[]" value='other' />Other
<input id='productionDownOther' name='other-production-down' type='text'/>
并插入使用implode
$check=$_POST['check'] //it is array of checkbox value
//convert in in string with comma seprated
$new_check=implode(",",$check);
答案 2 :(得分:0)
你必须像这样使用名称作为数组
<html>
<head>
<?php if(isset( $_POST['production-down'])) {
$name = $_POST['production-down'];
foreach ($name as $color){
if($chk == "other" )
$chk=$_POST['production-down-name'];
echo $chk."<br />"; } }?>
</head>
<body>
<form method="POST">
<p class='question'>Reasons why it is down. Click as many as apply.</p>
<input type='checkbox' name='production-down[]' value='Decrease in demand' required>Decrease in demand <br>
<input type='checkbox' name='production-down[]' value='Expected Decrease in demand'>Expected Decrease in demand <br/>
<input type='checkbox' name='production-down[]' value='Technical difficulties in production'>Technical difficulties in production <br/>
<input type='checkbox' name='production-down[]' value='Shortage in raw materials'>Shortage in raw materials <br/>
<input type="checkbox" name="production-down[]" value='other' />Other
<input id='productionDownOther' name='production-down-name' type='text'/>
<input type="submit" name="submit" value="submit"/>
</form>
</body>
</html>