繁体   English   中英

Ajax将表单发布到Laravel中的控制器(存储)

[英]Ajax post a form to controller (store) in Laravel

我设法创建了一个表单,将PHP中的数据发布到商店控制器。 它运行良好,但现在我尝试将其转换为可以发出Ajax请求的位置。

我不能使它工作。 单击提交时,没有消息,也没有页面刷新,也没有数据存储。 Google CHrome开发人员工具的网络标签显示浏览器向Leads控制器发出了发布请求。 这就是我得到的,什么地方出了问题?

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部分

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(商店)

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 );
}

感谢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 );

    }
}

并且我还修改了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;
    }); 
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM