This is an extended question from: Get $_POST from multiple checkboxes

I am trying to get the labels of each checked checkbox, then display it as such:

"Sales | Engineering" (without the quotes)

Or if only one checkbox was selected:

"Sales"

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>

*Edited - the "if" statement in the php file always returns false even if multiple checkboxes are checked

有帮助吗?

解决方案

You should use array type naming loc_chks[], this also lets you use specific keys for each item, for example: loc_chks[sales] and with PHP you have it in $_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/>

In PHP you can use implode funciton, which lets you concatenate array items with the glue string (as first parameter)
PHP (result.php):

<?php

session_start();

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

?>

其他提示

Value is missing... use following

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

First of all assign those labels as values of checkboxes and secondly name the checkboxes like an array. Without naming all your checkboxes like that, you won't get multiple values like you are expecting in your loop.

<label for='sales'>Sales</label>
<input type='checkbox' name='loc_chks[]' id='sales' value="Sales"/><br/>
                                     ^^             ^       

Nevermind - Im as dumb@$$. I didn't close out the "name" property in the form. Everything works - Thank you all for your input. George PHP has a slightly better solution.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top