在你的路线中,
Route::get('data', array('uses' => 'HomeController@store'));
在 HomeController 中,
public function store() {
$input = Input::all(); // form data
// validation rules
$rules = array(
'email' => 'required|email',
'name' => 'required',
);
$validator = Validator::make($input, $rules); // validate
// error handling
if($validator->fails()) {
if(Request::ajax()) { // it's an ajax request
$response = array(
'response' => 'error',
'errors' => $validator->errors()->toArray()
);
} else { // it's an http request
return Redirect::intended('data')
->withInput()
->withErrors($validator);
}
} else { // validated
// save data
}
}
最后是剧本,
var root_url = "<?php echo Request::root(); ?>/"; // put this in php file
$('#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>');
});
});