Search code examples
jqueryajaxlaravellaravel-3

Laravel 3 issues with loading Views using Ajax


I have a page with multipele <FORM>s

eg:

@foreach($build['products'] as $productid)

<form id="productid_49" method="POST" action="/products/49/custom">                                                        
<input type="hidden" name="csrf_token" value="">
<input type="button" id="btn_49" value="Click">                                                        
</form>
<div id="form_output_49">
</div>

<form id="productid_23" method="POST" action="/products/23/custom">                                                        
<input type="hidden" name="csrf_token" value="">
<input type="button" id="btn_23" value="Click">                                                         
</form>
<div id="form_output_23">
</div>

<form id="productid_15" method="POST" action="/products/15/custom">                                                        
<input type="hidden" name="csrf_token" value="">
<input type="button" id="btn_15" value="Click">                                                         
</form>
<div id="form_output_15">
</div>
.
.
.
.
.
.
.
so on

@endforeach

When the btn is clicked, Jquery is called:

$('input#btn_'+productid).click( function(e) {
                e.preventDefault();
                var form = $('#productid_'+productid);
                var post_url = form.attr('action');
                var post_data = form.serialize();
                var div=$('#form_output_'+productid).html();

                //console.log(post_url);
               $.ajax({
                    type: 'POST',
                    url: post_url,
                    cache : false, 
                    data: post_data,
                    success: function(data) {
                        $.post(post_url, function(data){
                            $('#form_output_'+productid).html(data);
                        });   
                    }
                });      
        });

Routes:

Route::post('products/(:any)/custom', array('as' => 'compare', 'uses' => 'compares@custom'));

Controller:

public function post_custom($productid)
{
      $compare = new Compare($product_id);   
      $compare->customsingelproduct($product_id);
}

Lib files:

public function customsingelproduct($product_id)
{
    $view = View::make('view.ajaxtest');
    return $view;
}

It works fine but I cant seem to get the partial views, so that I can load only that and not to reload the entire page.

There is render() but it does not work.

How can I get a partial view of the same page using AJAX and basically do $('#form_output_'+productid).html(data); or something

I hope I clearly describes the issue, if not please ask.

need help

Working with Laravel 3.


Solution

  • Create partial view and load normally using Jquery:

    $('#form_output_'+productid).html(data);