1

これは拡張された質問です:複数のチェックボックスから $_POST を取得する

チェックされた各チェックボックスのラベルを取得して、次のように表示しようとしています。

"Sales | Engineering" (引用符なし)

または、チェックボックスが 1 つだけ選択されている場合:

「販売」

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 を返します。

4

4 に答える 4

2

配列タイプの命名 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では funcitonimplodeを使用できます。これにより、配列項目をグルー文字列 (最初のパラメーターとして)
PHP (result.php)と連結できます。

<?php

session_start();

 if(!empty($_POST["loc_chks"]) and is_array($_POST["loc_chks"])) {
     $loc = implode(' | ',$_POST["loc_chks"]);
 }

?>
于 2013-10-04T13:56:37.753 に答える
1

まず、これらのラベルをチェックボックスの値として割り当て、次にチェックボックスに配列のような名前を付けます。すべてのチェックボックスにそのような名前を付けないと、ループで期待するような複数の値を取得できません。

<label for='sales'>Sales</label>
<input type='checkbox' name='loc_chks[]' id='sales' value="Sales"/><br/>
                                     ^^             ^       
于 2013-10-04T13:54:10.030 に答える
1

値がありません...次を使用してください

<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/>
于 2013-10-04T13:53:04.210 に答える