0

I have two select lists in html and I would like to call a php function each time one value of both of these list changes. I already made the javascript file using ajax to get these values for each change. Now I would like to use these values to call my function each time there is a change as well. I'm using a POST request and a post route to get my data.

Here is my ajax.js file :

var year = $('#year option:selected').text();
var site = $('#site option:selected').val();

$.ajax({
    type: 'POST',
    url: 'previsionnel',
    data: {
        'year': year,
        'site': site
    },
    success: function (data) {
        $('#annualBudget').html(data);
    },
    error: function () {
        alert('error');
    }
}); 

$('#year, #site').change((function () {
    $('#year').attr('selected');
    $('#site').attr('selected');
    var year = $('#year option:selected').text();
    var site = $('#site option:selected').val();
    $.ajax({
        type: 'POST',
        url: 'previsionnel',
        data: {
            'year': year,
            'site': site
        },
        success: function (data) {
            $('#annualBudget').html(data);
        },
        error: function () {
            alert('error');
        }
    });
}));

Here is the routes.php file and the route where I call my function :

$app->post('/previsionnel', function (Request $request) use ($app) {
    $year = $request->request->get('year');
    $site = $request->request->get('site');
    $app['dao.annualBudget']->getAnnualBudgetByYearAndSite($year, $site);
    //return $app['twig']->render('previsionnel.html.twig', array('annualBudget' => $annualBudget));
});

For this code I have an 500 Internal Server Error for each POST requests made. After researches, I think I did an error somewhere or that's might be a server problem somehow. I want you to know that I'm not exactly sure it works this way so I might be wrong about how to get my data and then call my function.

thomashoareau
  • 99
  • 1
  • 1
  • 10
  • 2
    A 500 error is simply a Server error and can mean many things. Most likely, it's some issue with your PHP-code. Check your error log to get the _actual_ error message. While developing, you could also display all errors directly on the screen, here's how: https://stackoverflow.com/questions/5438060/showing-all-errors-and-warnings - Without the actual error message, we would all just be guessing. – M. Eriksson Jul 18 '17 at 06:24
  • You could try to change the method to GET, and manually request the ```/previsionnel?year=2017&site=whatever``` and see what's returning. But first of all, as @MagnusEriksson told you, you should check your error log (you'll quickly find what's wrong with your code instead of guessing what may be wrong :-) – mTorres Jul 18 '17 at 06:47
  • I checked my php.ini file, everything is fine in terms of displaying errors. I tried the GET request, it works but I still can't get my data to use it in my function afterwards. I use `$year = $request->query->get('year');` and it returns null in query so idk what else I can do to get them. – thomashoareau Jul 18 '17 at 08:44

0 Answers0