In your route
Route::get('data', array('uses' => 'HomeController@store'));
In the HomeController,
public function store() {
$input = Input::all();
$rules = array(
'email' => 'required|email',
'name' => 'required',
);
$validator = Validator::make($input, $rules);
if($validator->fails()) {
if(Request::ajax()) {
$response = array(
'response' => 'error',
'errors' => $validator->errors()->toArray()
);
} else {
return Redirect::intended('data')
->withInput()
->withErrors($validator);
}
} else {
}
}
And finally, the script,
var root_url = "<?php echo Request::root(); ?>/";
$('#newPost :submit').click(function(e){
var BASE = root_url + 'data';
e.preventDefault();
$.post(BASE, {
'message' : $('#newPost textarea.message').val()
}, function(data) {
$('#content').prepend('<p>' + data + '</p>');
});
});
source
share