Home > Web Front-end > JS Tutorial > body text

How to submit a form with ajax in Lavarel framework

php中世界最好的语言
Release: 2018-04-02 17:27:58
Original
1344 people have browsed it

This time I will show you how to submit a form with ajax in the Lavarel framework. What are the precautions for submitting a form with ajax in the Lavarel framework? The following is a practical case. Get up and take a look.

laravelIntroduction

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 post form, it needs to be added {{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:

<meta name="_token" content="{{ csrf_token() }}"/>
Copy after login

2 , and then add

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

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 best is the latter, you can get the data in the form and transmit it directly through ajax, which is simply amazing!!! (The 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+'<br>'+data[5].value+' | '+data[6].value+' | '+data[7].value);
        }
      },
    });
    // event.preventDefault();
    return false;
  });
Copy after login

3 Then in controllerTo get the data in the 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";
  }
Copy after login

I believe you have mastered the method after reading the case in this article , for more exciting content, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How does Ajax operate post request to jump to the page

How to handle when Ajax opens a new window and is intercepted

The above is the detailed content of How to submit a form with ajax in Lavarel framework. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!