dimanche 18 mars 2018

POST array of checkbox values

I'm making a website for my uni course so it's somewhat basic in a few places, but my current aim is to create an array of values based on checkboxes. This will become a basic basket/shopping cart so that the user can list the item's they want to take to the checkout.

I'm having trouble posting the values from my checkboxes though. I can only post one at a time, but what I intend to do is select multiple images at once and then add them in one go.

This is spread out over a few pages now and it's quite dynamic. Here are the relevant sections of code that I'm working with:

PhotographerAccount (displays the gallery based on the photographer you are viewing)

<div class="container">
<!--Form to select items to put in "basket"-->
<form action="basket.php" method="POST">
<!--Gallery-->
<div style="margin-bottom:50; margin-top:50; height:600px; border:3px solid #adadad; border-radius: 10px; overflow-y: auto; overflow-x: hidden; background-color: #e0e0e0">
   <?php include "personalGallery.php" ?>
</div>
<button type="submit" class="btn btn-block btn-info btn-lg">Add selected to basket (<small id="y">0</small>)</button>
</form>
</div>

personalGallery (creates the gallery and the checkboxes)

if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
    $photoID = $row['uploadID'];
    $photoNm = $row['photoName'];

    //Photo styling
    echo "<div class='col-sm-3 text-center' style='background-color:#e5e5e5; margin-bottom: 20px; border-width: 0 2 0 2; border-style: solid; border-color: #e0e0e0'>";

    //Checkbox for form and Photo name as a label
    echo "<div class='checkbox'>";
    echo "<label><input onclick='updateCount()' class='z' type='checkbox' name='selection[]' value='".$photoID."'><strong>".$photoNm."</strong></label>";
    echo "</div>";

    //Photo
    echo "<img src='photographersarea/PhotoUploads/".$photoID."' alt='".$photoNm."' style='height:200px;max-width: 100%'>";

    //Close Styling
    echo "</div>";

  }
} else {
echo "0 results";
}

From here the form should be posting the data to basket.php, but when I test the array using print_r($_POST['selection'] or even print_r($_POST) I only see value. From the things I've read this should have made an array that I can work with.

Any ideas how I can post the data as an array?

Thanks! (Will update post and make note of updates if needed)

UPDATES - Fixed typo




Aucun commentaire:

Enregistrer un commentaire