要一個完整的通用例子擴大@陳津林回答,適用在Laravel:
首先,你的代碼應該沒有任何JavaScript的工作,所以你需要擴展你已有的東西。
做一個途徑聽Ajax請求(post或get哪個更適合你):
Route::post('helpers/ajax',
array('as' => 'ajax', 'uses' => 'App\Controllers\[email protected]')
);
使用jQuery,你將停止表單提交的默認功能,並將其發送給你的Ajax URI
$('#yourSubmitButton').on('click', function(e){
e.preventDefault(); // the form will not be submitted
//do whatever necessary to collect the data, or just serialize the form
var formdata = $('#yourForm').serialize();
//perhaps validate the data, if you need, and then send by ajax
$.ajax({
url:'helpers/ajax',
type:'POST', //or GET if you wish as long as its consistent with the route
data: formdata,
dataType:'json', //this is for the data you will receive from the controller
cache:false,
success:function(data){
//show the "mail send message and whatnot
},
error:function(jxhr){
//handle errors
} })
})
缺少的是控制器上的方法,用於接收發布數據,驗證,處理和迴應請求(最可能是json編碼數組)。
更多內容是特定於您的實施。
你可以使用一個xajax函數。 – mercy