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.