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

How to submit a form using ajax in Lavarel framework_AJAX related

亚连
Release: 2018-05-23 10:25:45
Original
1670 people have browsed it

Because laravel needs to add {{csrf_field()}} to prevent cross-site attacks when submitting data in the form of a post. Let’s share with you how to use ajax to submit a form in the lavarel framework through this article. Let’s take a look.

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 post form, it needs to be added {{csrf_field()}} to prevent cross-site attacks, so it is natural to add it when you use ajax to submit a form.

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

1. First, add a meta to the template:

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

2, then add

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

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

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

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(&#39;uid&#39;,$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

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

The solution to the problem that the value of the Ajax submission parameter containing the html tag cannot be submitted successfully

Ajax post request jump page

Two solutions for Ajax opening a new window and being intercepted by the browser

The above is the detailed content of How to submit a form using ajax in Lavarel framework_AJAX related. 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!