Reputation: 8726
I'm trying to learn the Laravel Ajax request.
route.php
Route::get('/repository/', 'RepositoryController@index');
Route::post('/repository/', function(){
if(Request::ajax()){
return 'Got everything';
}
});
and
repository.index.php
I have a modal which is appearing upon clicking a button on the same page with the following markup
@section('popups')
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
<input type="text" name="name">
</div>
<a href="#" id="add">Submit</a>
<div class="modal-footer">
<button type="submit" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" name="add" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){
$('.add').click(function(e){
e.preventDefault();
var name=$(this).find('input[name=name]').val();
//post Ajax
$.post('/repository', {name:name}, function(data){
console.log(data);
});
});
});
</script>
@stop
Now, it looks like I cannot get the data in the console. What is the best way to send the data to the database? and if there is error in validation how can I show it in the model itself?
Upvotes: 0
Views: 1823
Reputation: 10794
Update your jQuery selector:
$(document).ready(function(){
$('#add').click(function(e){ // id=add
e.preventDefault();
var name=$(this).find('input[name=name]').val();
//post Ajax
$.post('/repository', {name:name}, function(data){
console.log(data);
});
});
});
You're looking for a class in your original code, whereas the anchor has an id! :)
Upvotes: 1