Ajax 将表单发布到 Laravel 中的控制器(商店)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26170890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ajax post a form to controller (store) in Laravel
提问by Lucien Dubois
I managed to create a form that post data in PHP to a store controller. It worked well but now I try to convert it so that it could make an Ajax request instead.
我设法创建了一个表单,将 PHP 中的数据发布到商店控制器。它运行良好,但现在我尝试转换它,以便它可以发出 Ajax 请求。
I can't make it work. When I click submit, I got no message, no page refresh and no data is stored. The network tab of the Google CHrome developer tools show that the browser makes a Post request to the Leads controller. Here is what I got, what is wrong?
我不能让它工作。当我点击提交时,我没有收到任何消息,没有页面刷新,也没有存储任何数据。Google CHrome 开发人员工具的网络选项卡显示浏览器向 Leads 控制器发出 Post 请求。这是我得到的,有什么问题吗?
create.blade.php (view)
create.blade.php(查看)
{{ Form::open(['route'=>'leads.store', 'method'=>'post', 'class'=>'formcontainer', 'id'=>'leadscreate']) }}
{{ Form::label('nomopportunite','Nom du lead')}}
{{ Form::text('nomopportunite', '', array('id'=>'nomopportunite1', 'class'=>'form-control', 'placeholder'=>'Nom du lead')) }}
{{ Form::label('statut','Statut')}}
{{ Form::select('statut', array('1' => 'Premier contact', '2' => 'En négociation', '3' => 'Fermé - Gagné', '4' => 'Fermé - Perdu'), '', array('id'=>'statut1')) }}
{{ Form::label('valeur','Valeur')}}
{{ Form::text('valeur', '', array('id'=>'valeur1', 'class'=>'form-control', 'placeholder'=>'Valeur ($)')) }}
{{ Form::submit('Ajouter', array('class'=>'btn btn-primary')) }}
{{ Form::close() }}
javascript ajax part
javascript ajax 部分
jQuery( document ).ready(function() {
$('#leadscreate').on('submit', function(){
$.post(
$(this).prop('action'), {
"_token": $( this ).find( 'input[name=_token]' ).val(),
"nomopportunite": $( '#nomopportunite1' ).val(),
"statut": $( '#statut1' ).val(),
"valeur": $( '#valeur1' ).val()
},
function(data){
//response after the process.
},
'json'
);
return false;
});
});
LeadsController.php (store)
LeadsController.php(商店)
public function store() {
if ( Session::token() !== Input::get( '_token' ) ) {
return Response::json( array(
'msg' => 'Erreur!'
) );
}
$nomopportunite = Input::get( 'nomopportunite' );
$statut = Input::get( 'statut' );
$valeur = Input::get( 'valeur' );
$response = array(
'status' => 'success',
'msg' => 'L\'opportunité a bien été ajoutée!',
);
return Response::json( $response );
}
回答by Lucien Dubois
Thank you to The Alpha for pointing out that there was something missing in my controller. I modified my controller to this and now it works (data is saved in the databse). Hope it will help.
感谢 The Alpha 指出我的控制器中缺少某些东西。我修改了我的控制器,现在它可以工作了(数据保存在数据库中)。希望它会有所帮助。
public function store() {
if ( Session::token() !== Input::get( '_token' ) ) {
return Response::json( array(
'msg' => 'Erreur!'
) );
}
$response = array(
'status' => 'success',
'msg' => 'L\'opportunité a bien été ajoutée!',
);
$rules = array(
'nomopportunite' => 'required',
'statut' => 'required',
'valeur' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::back()
->withInput()
->withErrors($validator);
} else {
$lead = new Lead;
$lead->nomopportunite = Input::get('nomopportunite');
$lead->statut = Input::get('statut');
$lead->valeur = Input::get('valeur');
$lead->save();
return Response::json( $response );
}
}
and I also modified my jQuery script to give the user a feedback:
我还修改了我的 jQuery 脚本来给用户一个反馈:
jQuery( document ).ready(function() {
$('#leadscreate').on('submit', function(){
$.post(
$(this).prop('action'), {
"_token": $( this ).find( 'input[name=_token]' ).val(),
"nomopportunite": $( '#nomopportunite1' ).val(),
"statut": $( '#statut1' ).val(),
"valeur": $( '#valeur1' ).val()
},
function($response){
$('#messagetop').slideToggle();
},
'json'
);
return false;
});
});