0

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.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Malarivtan
  • 404
  • 1
  • 6
  • 20
  • I can not load partial view into the page using AJAX – Malarivtan Sep 25 '16 at 09:43
  • Yes you explained in a very basic way what the problem is... but you are not explaining what is happening for you so far. You have `return Response::json($view));` so you're returning the page as JSON. So you need `dataType: "json"` in your AJAX options for one – Jonathan Sep 25 '16 at 09:44
  • it returns: objects: blabla when I counsol.log but does not load the view – Malarivtan Sep 25 '16 at 09:47
  • 1
    Ah yeah of course... so don't return it as JSON. Return it as you would a regular page. Try `return View::make('view.ajaxtest');` instead in this case... – Jonathan Sep 25 '16 at 09:49
  • @Jonathan it gives: 500 (Internal Server Error) – Malarivtan Sep 25 '16 at 09:54
  • Check your `laravel.log` and report the error – Jonathan Sep 25 '16 at 09:55
  • @Jonathan u is hiting the right spot., I will feed back u if it works nice. need to recharge my lap – Malarivtan Sep 25 '16 at 10:02
  • @Jonathan I am getting Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknown on line 0 – Malarivtan Sep 25 '16 at 10:20
  • http://stackoverflow.com/questions/26261001/warning-about-http-raw-post-data-being-deprecated - one thing to try reading some of the comments there would be to log out your post data and see that it is being sent properly – Jonathan Sep 25 '16 at 10:22
  • If you resolved with my help then I'd appreciate you gave a status update so I can fill in the correct answer and we can resolve this question? – Jonathan Sep 25 '16 at 12:36
  • @Jonathan Well so far I have solved the issue with loading. however, I still have few problems: The buttons do not work – Malarivtan Sep 26 '16 at 06:36
  • If you're original question is answered with my help then I appreciate if you update what the 500 was. I assume just a syntax error otherwise. You need to ask another question if you are having other problems. We don't help on stack overflow for free. Rep is due – Jonathan Sep 26 '16 at 06:53
  • @Jonathan it worked fine, had few issues but solved it. – Malarivtan Sep 26 '16 at 13:22
  • I didn't understand why you have double ajax request nested? When you have $.ajax success you have another post request to same url – serdar.sanri Nov 11 '16 at 20:54

1 Answers1

-1

Create partial view and load normally using Jquery:

$('#form_output_'+productid).html(data);
Malarivtan
  • 404
  • 1
  • 6
  • 20