jeudi 28 janvier 2016

Using checkbox's to construct URL then generate query with controller

I am trying to generate a URL dependent on which checkbox is checked, so on the click of a checkbox a URL is generated with parameters that I can grab and construct a query for a symfony controller. So Far I only have the html with basic php

<div class="side-bar">
        <h3>Categories</h3>
        <form >
            <?php foreach ($article_cats as $category) {
                        ?> <input type="checkbox" name="category[]" value="<?php echo $category['id'] ?>"> <?php
                }
            ?>
        </form>
</div>

Then I am trying to use jQuery/javascript to grab the values from the form but I cant seem to get any results

<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>

<script>
$(document).ready(function () {

    var categories = [];

    // Listen for 'change' event, so this triggers when the user clicks on the checkboxes labels
    $('input[name="cat[]"]').on('change', function (e) {

        e.preventDefault();
        categories = []; // reset

        $('input[name="cat[]"]:checked').each(function()
        {
            categories.push($(this).val());
        });

    });

// get data from array 
// construct url
// send to controller

});


</script>

Once I have the parameters, should be simple enough to generate the query for the controller. I just dont have any idea how construct the URL




Aucun commentaire:

Enregistrer un commentaire