My controller:
**
* @Route("/", name="homepage")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function indexAction(Request $request)
{
return $this->render('default/index.html.twig');
}
/**
* @Route("/title/{title}", name="Title")
* @param Request $request
* @param $title
* @return \Symfony\Component\HttpFoundation\Response
*/
public function TitleAction(Request $request, $title)
{
//Some DB calls to retrieve data with 'where .. = $title'
return $this->render('default/title.html.twig', array(
'title' => $title,
'results' => $results[0],
'cs' => $cs,
'gs' => $gs,
...
}
}
/**
* @param Request $request
* @return JsonResponse
*/
public function updateDataAction(Request $request)
{
//Here I also want to use $title so I can use it in my query
$dataCS = $request->get('inputCS');
$dataGS = $request->get('inputGS');
if(!empty($dataGS) and !empty($dataCS)) {
// $results = Query
}
else {
// $results = Query
}
return new JsonResponse(array('results' => $results[0]));
}
My AJAX call in script.js:
$(document).ready(function () {
$("#filter1").change(function() {
var inputCS = $(this).val();
if(inputCs.length >= 1) {
var data = {inputCS: inputCS};
$.ajax({
type: "POST",
url: ROOT_URL + "default/update/data",
data: data,
dataType: 'json',
timeout: 3000,
success: function(results){
//some innerHTML replacements
if(result.low === null) {
document.getElementById("low").innerHTML = 'N/A'
}
else {
document.getElementById("low").innerHTML =
Math.floor(Number(result.result.low));
}
},
error: function() {
//
}
})
}
});
})
My twig: Filters:
<select id="filter1">
<option selected disabled>Choose</option>
{% for c in cs %}
<option value="{{ c.name }}">{{ c.name }}</option>
{% endfor %}
</select>
<select id="filter2">
<option selected disabled>Choose</option>
{% for g in gs %}
<option value="{{ g.name }}">{{ g.name }}</option>
{% endfor %}
</select>
Where result is used:
{% if results.low != 0 %}
<p class="amount" id="low">{{ results.low|round(0, 'floor') }}</p>
{% else %}
<p class="amount">N/A</p>
{% endif %}
A bit background info: With function TitleAction I show some results on the twig page that is based on the value from the route (title/{title}) - with the help of some DB calls (with a where
clause). On this page I have 2 filters which you can use to filter the results that are shown on the page. When a filter is triggered (value selected) I do an AJAX call to the updateDataAction function where I do again some specific DB calls, but with an extra where
clause and the filter as value. I return the filtered result set with a JSON respons to my AJAX call. At the success I do some interHTML replacements on the original values.
My question is the following. How can I pass or use the same value {title} from the route (title/{title}) so I can also here use that specific value in my query.
I tried to also use the @route
annotation on that function, but no results and the $request->query...
didn't help either.
If I've understood your question correctly, what I'd do is have the {{results.title}}
in an HTML attribute that you can read with your JS when making the AJAX request.
eg
<body data-title="{{results.title}}">
Then in your JS:
$("#filter1").change(function() {
var inputCS = $(this).val();
var title = $('body').data('title');
if(inputCs.length >= 1) {
var data = {
inputCS: inputCS,
title: title
};
$.ajax({
type: "POST",
url: ROOT_URL + "default/update/data",
data: data,
dataType: 'json',
timeout: 3000,
success: function(results){
// ...
},
error: function() {
//
}
});
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.