I try to post data to laravel controller using ajax, but still get response null. My achievement is to post form data and return error message or success message. I'm newbie in ajax and laravel framework please help me to solve the problem.
Here is the meta tag header:
<meta name="_token" content="{{ csrf_token() }}">
Here is the html form :
{{ Form::open(['id'=>'testimonial_form','url'=>URL::action("processing-give-testimonial"),'method'=>'POST']) }}
{{ Form::hidden('_method', 'PUT') }}
<div class="row marginbot-20">
<div class="col-md-6 xs-marginbot-20">
{{ Form::text('name',null, ['class'=>'form-control input-lg','placeholder'=>'Enter Name','id'=>'name']) }}
</div>
<div class="col-md-6">
{{ Form::email('email',null, ['class'=>'form-control input-lg','id'=>'email','placeholder'=>'Enter email','id'=>'email']) }}
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<select name="subject" id="subject" class="form-control" require="required">
<option value="ask">Ask Question / Information</option>
<option value="testimonial">Give Feedback / Testimonial</option>
</select>
</div>
<div class="form-group">
{{ Form::textarea('message',null,['id'=>'message','class'=>'form-control','rows'=>'4','cols'=>'25','placeholder'=>'message','id'=>'message']) }}
</div>
<!-- {{ Form::submit('Send Message',['id'=>'btnContactUs','class'=>'btn btn-skin btn-lg btn-block']) }} -->
{{ Form::button('Submit', ['class'=>'btn btn-skin btn-lg btn-block','id'=>'click']) }}
</div>
</div>
{{ Form::close() }}
Here is the ajax code :
$(function() {
$.ajaxSetup({
headers: {
'X-XSRF-Token': $('meta[name="_token"]').attr('content')
}
});
});
$(document).ready(function() {
$('#click').click(function() {
var formData = {
name : $('#name').val(),
email : $('#email').val(),
subject : $('#subject').val(),
message : $('#message').val(),
};
$.ajax({
type : "POST",
url : "{{ URL::action('processing-give-testimonial') }}",
data : formData,
beforeSend: function () {
alert('sure?');
},
success: function(data) {
console.log(data);
},
error: function() {
console.log('error');
}
});
});
});
Here is the controller :
public function create()
{
$inputs = array(
'name' =>Input::get('name'),
'email' =>Input::get('email'),
'subject' =>Input::get('subject'),
'message' =>Input::get('message')
);
//return "we reached here";
return Response::json("success");
/*if(Request::ajax()) {
return Response::json('success', 200);
} else {
return Response::json('failed', 400);
}*/
/* if(Request::ajax()) {
$data = Input::get('email');
//print_r($data);die;
if ($data != '') return Response::json('success',200);
else return Response::json('failed',400);
}*/
/*
$input = Input::get('name');
//$input = Input::get('_token');
if ($input == '') {
return Response::json('failed',400);
}
else {
return Response::json('success',200);
}*/
//if(!empty($input)) return Response::json(['data'=>'success']);
//else return Response::json('data',$input);
}
Here is the my route :
Route::post('give-testimonial',['uses'=>'TestimonialController@store','as'=>'processing-give-testimonial']);
Here is the filter.php :
Route::filter('csrf', function() {
$token = Request::ajax() ? Request::header('X-XSRF-Token') : Input::get('_token');
if (Session::token() != $token) {
throw new Illuminate\Session\TokenMismatchException;
} });