javascript复选框(单击图像 - 复选框)帮助w / php

时间:2017-10-03 09:53:23

标签: javascript php html

我有以下内容,我想添加一个功能,允许我点击显示的图像,上面的复选框变为选中状态,是否可能?似乎第29-32行。我找到了一个例子,但对我来说没有用。

感谢



<html>
<head>
    <title>This is a simple PHP script to delete select images.</title>
</head>
<body>
    <?php
        if (isset($_POST['image_list'])) {
            foreach ($_POST['image_list'] as $imagename) {
                if (file_exists($imagename)) {
                    unlink($imagename);    
                }
            }
        }
    ?>
    <form action="images_view.php" method="POST">
        <p>Please select multiple images you want to remove. Please note that the selected images will be removed from server as well.</p>
        <?php
            $files = glob("videos/THUMBNAILS/*.*");
            for ($i=0; $i<count($files); $i++)
            {
                $image = $files[$i];
                $supported_file = array(
                    'jpg',
                );


                $ext = strtolower(pathinfo($image, PATHINFO_EXTENSION));
                if (in_array($ext, $supported_file)) {
                    echo '<input type="checkbox" name="image_list[]" style="margin-right:10px;" value="'.$image.'" />';
                    echo '<input type="checkbox" name="image_list[]" style="margin-right:10px;" value="'.$image.'" />';
                    echo basename($image)."<br />"; // show only image name if you want to show full path then use this code // echo $image."<br />";
                    echo '<img src="'.$image .'" style="max-width: calc(100% - 20px); alt="Random image" />'."<br /><br />";
                } else {
                    continue;
                }
            }
        ?>
        <input type="submit" name="submit" style="width:300px;height:60px;margin-left:20px;" value="Delete" />
    </form>
</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您可以通过标签包装复选框输入和图像:

<label>
    <input type="checkbox" name="image_list[<?php echo $imageId; ?>]" />
    <img src="https://lh3.googleusercontent.com/ez8pDFoxU2ZqDmyfeIjIba6dWisd8MY_6choHhZNpO0WwLhICu0v0s5eV2WHOhuhKw=w170" />
</label>

Demo

在后端,你可以简单地做一个foreach并删除相应的图像:

foreach (array_keys($_POST['image_list']) as $imageId) {
    // delete image by id.
}