Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 具有相同名称属性的复选框和其他文本框组?_Javascript_Php_Jquery_Html - Fatal编程技术网

Javascript 具有相同名称属性的复选框和其他文本框组?

Javascript 具有相同名称属性的复选框和其他文本框组?,javascript,php,jquery,html,Javascript,Php,Jquery,Html,假设我有几个复选框选项: <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='pro

假设我有几个复选框选项:

<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'/>

它关闭的原因。尽可能多地单击“应用”

需求减少
预计需求减少
生产中的技术难题
原材料短缺
其他
如果我要选中
其他
复选框。我是否应该为
#productionDownOther
文本框指定与复选框相同的名称属性?应该是这样吗

我计划使用php方法
内爆
将所选值添加到一起,并将其插入数据库的一列中


请告诉我我的计划是否错误,如果是/否,请告诉我原因。我是个网络开发新手。仍然期待学习。

只需将名称属性更改为数组:

<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'/>

首先,您想允许多个选项吗?或者一次只允许一个复选框。如果是第一个选项,则需要在每个复选框名称的末尾添加
[]
,以便
生产停止
变为
生产停止[]
。这样,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

您必须使用名称作为数组来实现以下目的

<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>

为什么它会下跌。尽可能多地单击“应用”

需求减少
预计需求减少
生产中的技术难题
原材料短缺
其他
<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>