How to make the page not refresh after I submit with the help of using AJAX in LARAVEL 4?
//xx.blade.php
{{ Form::open(['url' => '/booking/unit', 'method' => 'POST', 'id' => 'readForm']) }}
{{ Form::hidden('bookingid', $booking->id) }}
{{ Form::hidden('unitid', $booking->unit) }}
<button class="btn btn-warning" data-toggle="collapse" data-target="#{{$booking->id}}" type="submit">{{ $booking->status }}</button>
{{ Form::close() }}
alternative way i tried:
<form method="post" action="../booking/unit/" id="readForm" >
<input value="{{$booking->id}}" hidden name="bookingid" />
<input value="{{$booking->unit}}" hidden name="unitid" />
<button class="btn btn-warning" type="submit" id="readBtn" >{{ $booking->status }}</button>
</form>
//js
$("document").ready(function(){
$("#readForm").submit(function(){
data = $(this).serialize();
$.ajax({
type: "POST",
dataType: "json",
url: "../booking/unit",
data: data,
success: function(data) {
alert("Form submitted successfully.\nReturned json: " + data["json"]);
}
});
return false;
});
});
my page is still refreshing. why?
UPDATES [22nd July 2015] I actually have multiple table rows with the forms and the forms are having the same id. that caused the refreshing happened except for the first row of the form. I have changed the form id to class and this is solved! Thank you everyone! You guys are awesome and the answers are valuable. I will be extra careful next time. :D
form submitlike$("#readForm").submit(function(event){event.preventDefault()});followed by your normal code!!