I have a gallery where there are images and next to the images are checkboxes. When for exmaple user click 3 of checkboxex and a submit button i would like to save these 3 images in session to show them in the other gallery (of chose images). Now my code looks like this:
<?php if ($images->count()): ?>
<?php foreach ($images as $image): ?>
<tr>
<td><?= $image['title'] ?></td>
<td><?= $image['author'] ?></td>
<td class="image">
<a href="static/images/<?= $image['file_name'] ?>">
<img class="gallery" src='static/images/<?= $image['file_name']?>'>
</a>
</td>
<td>
<form action="gallery" method="post" class="wide"/>
<input type="hidden" name="id" value="<?= $image['_id'] ?>"/>
<input type="checkbox" name="ckeckbox"/>
<input type="submit" name="gallery" value="Zapamiętaj"/>
</form>
</td>
</tr>
<?php endforeach ?>
<?php endif ?>
It works but the problem is, that all images have own buttons and I would like to have one universal button to accept all clicked checkboxes. How can I do this?
You have to move your form outside of the foreach loop. I also suggest you rename your checkbox input (hidden input is not usefull in this case but you can keep it if you want):
<?php if ($images->count()): ?>
<form action="gallery" method="post" class="wide"/>
<?php foreach ($images as $image): ?>
<tr>
<td><?= $image['title'] ?></td>
<td><?= $image['author'] ?></td>
<td class="image">
<a href="static/images/<?= $image['file_name'] ?>">
<img class="gallery" src='static/images/<?= $image['file_name']?>'>
</a>
</td>
<td>
<input type="checkbox" name="<?= $image['_id'] ?>"/>
</td>
</tr>
<?php endforeach ?>
<input type="submit" name="gallery" value="Zapamiętaj"/>
</form>
<?php endif ?>
<input type="hidden" name="id**[]**" value="<?= $image['_id'] ?>"/>
If you change name of the input to "id[]", it means it will be an array named "id". In the PHP script gallery (form action), where you add it to session, you can do it like this:
<?php
foreach($_POST["id"] as $id){
$_SESSION["selectedphotos"][] = $id;
}
?>
EDIT: And you have to move form before foreach...
<?php if ($images->count()): ?>
<form action="gallery" method="post" class="wide"/>
<?php foreach ($images as $image): ?>
<tr>
<td><?= $image['title'] ?></td>
<td><?= $image['author'] ?></td>
<td class="image">
<a href="static/images/<?= $image['file_name'] ?>">
<img class="gallery" src='static/images/<?= $image['file_name']?>'>
</a>
</td>
<td>
<input type="hidden" name="id" value="<?= $image['_id'] ?>"/>
<input type="checkbox" name="ckeckbox"/>
<input type="submit" name="gallery" value="Zapamiętaj"/>
</td>
</tr>
<?php endforeach ?>
</form>
<?php endif ?>
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.