1

I have this div:

@foreach($bids as $b)
<div class="bids notice notice-lg">
    <strong>{{$b->user->name}}</strong> siūlo <span style="font-size: 18px" class="label label-success">{{$b->bid}} €</span>
</div>
@endforeach

And this is my AJAX:

$(function(){

$('#placeBid').on('submit',function(e){
    $.ajaxSetup({
        headers: {
            'X-CSRF-Token': $('meta[name="csrf_token"]').attr('content')
        }
    });
    e.preventDefault(e);

        if($("#jsBidPrice").val() <= $(".minBid").val()){

        $.ajax({

        method:"POST",
        url: $("#placeBid").attr("action"),
        data:$(this).serialize(),
        dataType: 'text',
        success: function(data){  
           $('.minBid').val(0);
           $("#bidSubmit").attr("disabled", true);

        },
        error: function(data){
            console.log("request failed");
        }
    })
    }
        else {
            alert('Too low.');
        }
    });
});

How can I append my div #bids with new submitted data ? This is what I'm trying to right now - I'm trying to create new DIV and place it in foreach value. Like this:

var html = '<strong>{{$b->user->name}}</strong>';
html += 'siūlo <span style="font-size: 18px" class="label label-success">';
html += '54044 €</span>';

$('.bidsA').append(html);

And I place this div over the bids div. So the HTML looks like this:

@foreach($bids as $b)

<div class="bidsA notice notice-lg">
</div>


<div class="bids notice notice-lg">

    <strong>{{$b->user->name}}</strong>siūlo <span style="font-size: 18px" class="label label-success">{{$b->bid}} €</span>

</div>
@endforeach

And of course I get undefined variable $b how should I correctly include it from AJAX to HTML ?

EDIT

public function store(BidRequest $request, $id)
{   

     $product = Product::where('id', $id)->first();
     $bid = new Bid([
        'product_id' => $product->id,
        'bid'   => $request->bid,
     ]);

     Auth::user()->bid()->save($bid);

    }
}
12
  • You're merging php and js please try to clarify your specific problem and add additional details (like view code and controler)... Commented Mar 3, 2016 at 15:17
  • I need that when I submit form, the values from DB appears without page refresh. My AJAX inserts them in DB I just dont know how to show it after I submit form Commented Mar 3, 2016 at 15:26
  • Could you please add the action that insert values to DB, and view that show them. Commented Mar 3, 2016 at 15:39
  • @ZakariaAcharki I've added a function from BidController Commented Mar 3, 2016 at 15:45
  • What is the name of the view? Commented Mar 3, 2016 at 15:49

1 Answer 1

1

You could return the view and refresh the part that contain the bids.

In controller return $bids to view and it will construct the html with all bids divs :

public function store(BidRequest $request, $id)
{   
    ....

    $bids = Bid::orderBy('bid', 'DESC')->paginate(10);

    return view('products.show', compact('bids'));
}

Then in JS you will recieve the html you have just to append it to the parent container :

success: function(data){  
    $('.bids_container').replaceWith(data);
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

5 Comments

I'm getting error and not success response from AJAX now.
Yes that because you're using dataType: 'text' check my answer In your other question.
Okay I delete it and I still need to refresh page
What you mean, are you receiving html in success callback?
Can we discuss this in chat ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.