1

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?

1
  • 2
    You need to put the submit button outside of the foreach statement. Commented Jan 10, 2016 at 17:54

2 Answers 2

2

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 ?>
Sign up to request clarification or add additional context in comments.

2 Comments

Would it be more natural to process the return checkbox unique information as array values rather than array keys? i.e. would it be easier to have the unique_id in the checkbox value instead of the name?
@Ryan Vincent Absolutly. This is another solution.
1
<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 ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.