How to trigger a colorbox gallery with form submit

400 views Asked by At

I have a document with two pages. The first one sends a folder name via form submit

        <form method="post" >
            <input type="hidden" name="portfolio" value="directory">
            <input type="submit" value="author">
        </form>   

the second one receives this folder name and open the files with glob function

<?php
header('Content-Type: application/json');
$directoryName = $_POST['portfolio'];
echo json_encode (glob($directoryName."/".'*.{jpg,mp4}', GLOB_BRACE));
?>

...finally images appear on the first page again via Ajax

        $(document).ready(function(){
            $('form').submit(function(e) {
                var data = $(this).serialize();
                e.preventDefault();
                $.ajax({
                    url: "PAGE2.php",
                    data: data,
                    type: "POST",
                    dataType: "json",
                    success: function(response) {
                        var html = "";
                        $.each(response, function(i, val) {
                            var fileExtension = val.substring(val.lastIndexOf('.'));
                            if (fileExtension == ".jpg") {
                                html += '<img src="'+val+'" height=250></img>';
                            }
                            else if (fileExtension == ".mp4") {
                                html += '<video width="320" height="240" src="'+val+'" type="video/mp4"controls autoplay loop></video>';
                            }
                        });
                        $("#display").html(html);
                    }
                });
            });
        }); 

up to here no href tags... I need to know how to display images like modal gallery with colorbox plugin. I've tried this without success

        $(document).ready(function(){
            $('form').submit(function(e) {
                var data = $(this).serialize();
                e.preventDefault();
                $.ajax({
                    url: "PAGE2.php",
                    data: data,
                    type: "POST",
                    dataType: "json",
                    success: function(response) {
                        var html = "";
                        $.each(response, function(i, val) {
                            var fileExtension = val.substring(val.lastIndexOf('.'));
                            if (fileExtension == ".jpg") {
                                html += '<img src="'+val+'" height=250></img>';
                            }
                            else if (fileExtension == ".mp4") {
                                html += '<video width="320" height="240" src="'+val+'" type="video/mp4"controls autoplay loop></video>';
                            }
                        });
                        $("#display").html(html);
                        $("#link").colorbox({ inline:true, href: "#display"});
                    }
                });
            });
        }); 
<body>
    ... 
    <div id="display" style="display:none;"></div> 
    <a id="link" style="display:none"></a>
</body>

PAGE1.php

<!DOCTYPE HTML>
<html lang="">
<head>
    <meta charset="UTF-8">
    <title>page1</title>
    <link rel="stylesheet" href="colorbox.css" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="../jquery.colorbox.js"></script>
    <script>
        $(function() { 
            $("#formID").submit(function() {
                $.post($(this).attr("action"), $(this).serialize(), function(data) {
                    $.colorbox({
                        html:data,
                        rel:'group1'
                    });
                },
                       'html');
                return false;
            });
        });
    </script>
</head>
<body>
    <div class="container">
        <form action="page2.php" method="post" id="formID">
            <input type="hidden" name="var" value="directory">
            <input type="submit" value="author">
        </form>   
    </div>
</body>
</html>

PAGE2.php

<!DOCTYPE HTML>
<html lang="">
<head>
    <meta charset="UTF-8">
    <title>page2</title>
</head>
    
<body>
<?php
$variable = $_POST['var'];
$img_dir = $variable . "/";
foreach(glob($img_dir . '*.jpg') as $image) {
echo '<img src="'.$image.'">'.'<img class="group">';
}
?>
</body>
</html>

0

There are 0 answers