Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

load a twig template with ajax inside another one in symfony2

Tags:

symfony

I have a template with specific data, that has filter options for displaying articles. I want that when a user clicks on a filter option, another template will be loaded inside this template with AJAX that will display the articles based on the selected filter option. I've been searching on 'net but all I found was ambiguous and most was about returning a simple response and not a twig. Please help me with this issue.

Does anyone have a simple example on how to do that in Symfony 2?

like image 263
ZeSoft Avatar asked Sep 16 '25 23:09

ZeSoft


1 Answers

You can do like this this is your html form where ajax call

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script>
function SearchForm(){

var brand=$('#brand').val();

$.ajax({
        type:'POST',
        url: $('#url').text(),
        data: {brand: brand,},  
        success: function(response) {

       $('#all_data_search').html(response);
    }});
    return false;
}
</script>

<div id="all_data_search"></div>
<inputtype="button" onClick="SearchForm()">

and your controller you have to call this method

public function productSeachResultAction(Request $request){
$data = $request->request->all();
// What you want to do ///

 return $this->render('AdminBundle:Product:searchResult.html.twig',$productResult);    
 }

Create another template for result searchResult.html.twig Work you want to do her

<div>
a,b,c,
</div>
like image 184
Suresh Kumar Amrani Avatar answered Sep 18 '25 19:09

Suresh Kumar Amrani