簡體   English   中英

具有相同名稱屬性的復選框組和其他文本框?

[英]Group of checkbox and Other textbox with the same name attribute?

假設我有幾個復選框選擇:

<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開發的新手。 仍然期待學習。

首先,您要允許多個選項嗎? 或一次只允許一個復選框。 如果是第一個選項,則需要在每個復選框名稱的末尾添加[] ,以便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

請注意,在輸入數據庫之前,應始終清除數據。 同樣,檢查post值是否確實存在也是明智的,這樣PHP不會發出通知。

只需將name屬性更改為array即可:

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

您必須像這樣將名稱用作數組

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM