简体   繁体   中英

GET method with checkboxes - PHP

Im trying to display results of a checkbox on a separate page using php, however currently it displays as "array" instead of listing which boxes have been checked.

Here is my html:

Vegetarian <input type="checkbox" name="diet[]" value="Vegetarian">
Vegan <input type="checkbox" name="diet[]" value="Vegan">
Peanut Allergy <input type="checkbox" name="diet[]" value="PeanutAllergy">
Gluten Allergy <input type="checkbox" name="diet[]" value="GlutenAllergy">

Here is the php code:

<li><b>Dietary Requirements: </b> <?php echo $_GET['diet']; ?></li>

Id really appreciate any help on getting this working, im new to php!

You're spitting out an array in string context, meaning you're just going to see Dietary Requirements: Array , literally.

At minimum, you should implode() the array so it becomes a string:

<?php echo implode($_GET['diet']); ?>

and note directly dumping user-input into an output page like this is, is highly vulnerable to XSS attacks.

Depends, either loop through the array or implode() it:

foreach($_GET['diet'] as $diet) {
    echo $diet;
}

//or

echo implode(', ', $_GET['diet']);

use this

<li><b>Dietary Requirements: </b> 
<?php
    $checks = $_GET['diet'];
  foreach($checks as $check){
       echo $check."<br>";
  }
?>
</li>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM