lundi 27 juin 2016

Send array data through AJAX

I have the following issue.

I'm trying to send a AJAX call whenever a checkbox is checked. The value of the checkbox is send to my PHP file where the DB will be checked to get all the data of that value.

The problem is, I would like to show the data on the page but I don't know how.

Below is my code:

HTML:

<input type="checkbox" name="category[]" id="1" value="1" /> <label for="1">Administratieve Sector</label><br />
<input type="checkbox" name="category[]" id="2" value="2" /> <label for="2">Bouw Sector</label><br />
<input type="checkbox" name="category[]" id="3" value="3" /> <label for="3">Financiele Sector</label><br />
<input type="checkbox" name="category[]" id="4" value="4" /> <label for="4">Gezondheidszorg Sector</label><br />
<input type="checkbox" name="category[]" id="5" value="5" /> <label for="5">Horeca- en toerisme Sector</label><br />

JQuery:

function calculate() {

    var arr = $.map($('input:checkbox:checked'), function(e, i) {
        return +e.value;
    });

    $.ajax({ 
        url: '/company/test.php',
        data: {key: arr, i: 1},
        type: 'post',
        success: function(output) {
            obj = JSON.parse(output);

            console.log(obj);
            // Don't know where to go next
        }
    });
}

calculate();

$("div[class='col-md-12'").delegate("input:checkbox[name='category[]']", 'click', calculate);

PHP:

<?php

require_once '../core/init.php';

$v = new Vacatures();

if(isset($_POST['key']) && isset($_POST['i']) && !empty($_POST['key'])) {

    $key = $_POST['key'];
    $i = $_POST['i'];

    $vacs = $v->getFiltered($key, $i);

    echo json_encode($v->data());
    exit();
}

require_once VIEW_ROOT . '/company/test_view.php';

UPDATE (Content of obj)

[Object]0: Objectcategory: "1"company: "c1"content: "test"foto: "test"id: "2"region: ""title: "Test"__proto__: Objectlength: 1__proto__: Array[0]

Aucun commentaire:

Enregistrer un commentaire