Home  >  Article  >  Backend Development  >  Sharing examples of using ajax to submit forms in the Lavarel framework

Sharing examples of using ajax to submit forms in the Lavarel framework

小云云
小云云Original
2018-01-08 15:41:072008browse

Because laravel needs to add {{csrf_field()}} when submitting data in the form of post to prevent cross-site attacks. This article mainly shares with you the method of using ajax to submit the form in the lavarel framework. Let's take a look. I hope it can help everyone. .

Introduction to laravel:

Laravel is a simple and elegant PHP Web development framework (PHP Web Framework). It can free you from messy codes like noodles; it can help you build a perfect network APP, and every line of code can be concise and expressive. "Development" should be a creative mental work, not a boring "base code".

Let’s get straight to the point, because when Laravel submits data in the form of post, it needs to add {{csrf_field()}} to prevent cross-site attacks, so it is natural to add it when you submit the form using ajax.

I read a lot of solutions on the Internet. I solved it with the following method:

1. First, add a meta in the template:

2 , and then add

headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
},

in the ajax method. This is the ajax method. I found very useful jquery functions, $().serialize() and $().serializeArray(). I use them in the code. The latter is the latter. You can get the data in the form and transmit it directly through ajax. It’s amazing!!! (My ignorance makes everyone laugh)

$(form[1]).submit(function(event){
    var data = $(form[1]).serializeArray();
    // console.log(data);
    $.ajax({
      type:'post',
      url:'/basic',
      data:data,
      headers: {
  'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
},
      success:function(msg){
        if (msg) {
          $('.basicEdit').hide();
          $('.basicShow').show();
          $('.basicShow span').html(data[1].value+' | '+data[2].value+' | '+data[3].value+' | '+data[4].value+'
'+data[5].value+' | '+data[6].value+' | '+data[7].value);         }       },     });     // event.preventDefault();     return false;   });

3 Then get the data in the controller method Just $req->your form name.

public function basic(Request $req){
   // return $req->gender;
   $uid = Auth::user()->uid;
   // return $uid;
   // $inf = new \App\Info;
   $inf = Info::where('uid',$uid)->first();
   // return $inf;
   $inf->name = $req->name;
   $inf->gender = $req->gender;
   $inf->topDegre = $req->topDegre;
   $inf->workyear = $req->workyear;
   $inf->tel = $req->tel;
   $inf->email = $req->email;
   return $inf->save()?"ok":"fail";
  }

To summarize:

I think every step I mentioned is necessary!!!, my callback function The code written inside is to reprint the data obtained in the form. You can ignore the unnecessary ones, and then just look at the code, presented by a PHP beginner.

Related recommendations:

The Ajax submission form page refreshes very quickly. Example explanation

Example detailed explanation of jQuery Validator's method of verifying the Ajax submission form and Ajax parameter passing method

jquery implements two methods of ajax submission form

The above is the detailed content of Sharing examples of using ajax to submit forms in the Lavarel framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn